Best Python code snippet using autotest_python
mock_writer.py
Source:mock_writer.py
...23 def _check_for_exception(self, action_str):24 if (self.raise_exception_on is not None and25 self.raise_exception_on in action_str):26 raise self.mock_exception27 def _new_context(self):28 self.current_context_index += 129 return "context_" + str(self.current_context_index)30 def _new_get_value(self):31 self.current_get_value_index += 132 return "value_" + str(self.current_get_value_index)33 def set_validate_only(self, value=True):34 pass35 def is_validate_only(self):36 return False37 def set_return_empty_select_list(self, return_empty_select_list=True):38 self.return_empty_select_list = return_empty_select_list39 def encode_data(self, encode=False):40 self.encode = encode41 #42 # Implement all required Abstract Base Class prototype functions.43 #44 def start_session(self):45 """46 Starts a session with device47 """48 self._record_action("start-session")49 def stop_session(self):50 """51 Stops the session with device52 """53 self._record_action("stop-session")54 def create_object(self, object_name, context=None):55 """56 Creates an object in the current context, object is not saved to device57 """58 self._record_action("create-object %s [%s]" % (object_name,59 str(context)))60 return self._new_context()61 def update_object(self, object_name, by_field, value, context=None):62 """63 Updates an object in the current context, object is not saved to device64 """65 try:66 self.select_object(object_name, by_field, value, context)67 self._record_action("update-object %s %s = %s [%s]" % (object_name,68 by_field,69 str(value),70 str(context)71 ))72 except Exception:73 return self.create_object(object_name, context)74 return self._new_context()75 def select_object(self, object_name, by_field, value, context=None):76 """77 Selects an object in the current context78 """79 self._record_action("select-object %s %s = %s [%s]" % (object_name,80 by_field,81 str(value),82 str(context)))83 return self._new_context()84 def delete_object(self, context):85 """86 Deletes the object selected in the current context87 """88 self._record_action("delete-object [%s]" % str(context))89 return self._new_context()90 def set_values(self, context, **kwargs):91 """92 Sets values in the object selected in the current context and saves it93 """94 values = []95 for key in sorted(kwargs.keys()):96 val = kwargs[key]97 if type(val) is dict:98 sorted_dict = {k: val[k] for k in sorted(val)}99 values.append("%s=%s" % (key, str(sorted_dict)))100 else:101 values.append("%s=%s" % (key, str(val)))102 self._record_action("set-values %s [%s]" % (','.join(values),103 str(context)))104 return self._new_context()105 def unset_values(self, context, **kwargs):106 """107 Unsets values of a selected object when being reverted108 """109 values = []110 for key in sorted(kwargs.keys()):111 values.append("%s=%s" % (key, str(kwargs[key])))112 self._record_action("unset-values %s [%s]" % (','.join(values),113 str(context)))114 return self._new_context()115 def get_value(self, field, context):116 """117 Gets a value from the object selected in the current context118 """119 self._record_action("get-value %s [%s]" % (field, str(context)))120 if self.encode:121 return base64.b64encode(self._new_get_value().encode("ascii"))122 return self._new_get_value()123 def get_object_list(self, object_name, context=None):124 self._record_action("get-object-list %s [%s]" % (object_name,125 str(context)))126 if self.return_empty_select_list:127 return []128 context_1 = self._new_context()129 context_2 = self._new_context()130 return [context_1, context_2]131 def does_object_exist(self, context):132 """133 Return is the object already exists on the device or not134 """...
test_config.py
Source:test_config.py
...19class TestScenarioConfig(unittest.TestCase):20 def test_simple_config(self):21 config_content = "a = 'a'\n"22 config = ScenarioConfig(config_content)23 expected_context = self._new_context({'a': 'a'})24 self.assertEqual(expected_context, config.get_context_for_scenario('s1'))25 def _new_context(self, context=None):26 base_context = {'sipp_std_options': ''}27 if context is not None:28 base_context.update(context)29 return base_context30 def test_config_with_undefined_name_raise_exception(self):31 config_content = "a = b\n"32 self.assertRaises(Exception, ScenarioConfig, config_content)33 def test_config_with_bad_syntax_raise_exception(self):34 config_content = "!a\n"35 self.assertRaises(Exception, ScenarioConfig, config_content)36 def test_new_from_filename(self):37 config_filename = os.path.join(os.path.dirname(__file__), 'config', 'example1')38 config = ScenarioConfig.new_from_filename(config_filename)39 expected_context = self._new_context({'a': 'a'})40 self.assertEqual(expected_context, config.get_context_for_scenario('s1'))41 def test_config_using_scenarios_variable(self):42 config_content = """\43scenarios.s1 = dict(a='s1.a')44"""45 config = ScenarioConfig(config_content)46 expected_context = self._new_context({'a': 's1.a'})47 self.assertEqual(expected_context, config.get_context_for_scenario('s1'))48 def test_global_variables_doesnt_override_scenario_variable(self):49 config_content = """\50scenarios.s1 = dict(a='s1.a')51a = 'a'52"""53 config = ScenarioConfig(config_content)54 expected_context = self._new_context({'a': 's1.a'})55 self.assertEqual(expected_context, config.get_context_for_scenario('s1'))56 def test_hyphen_are_converted_to_underscore(self):57 config_content = "scenarios.s_1 = dict(a='s_1.a')\n"58 config = ScenarioConfig(config_content)59 expected_context = self._new_context({'a': 's_1.a'})60 self.assertEqual(expected_context, config.get_context_for_scenario('s-1'))61 def test_sipp_std_options_is_built(self):62 config_content = """\63sipp_local_ip = 'local_ip'64sipp_call_rate = 1.065sipp_rate_period_in_ms = 100066sipp_max_simult_calls = 367sipp_background = True68sipp_enable_trace_calldebug = True69sipp_enable_trace_err = True70sipp_enable_trace_shortmsg = True71sipp_enable_trace_stat = True72sipp_nb_of_calls_before_exit = 473"""...
AtomFeed.py
Source:AtomFeed.py
...18 subtype = 'atom'19 def __init__(self, ui_wrap_func, main, url, doc=None):20 root = doc.children21 self.namespace = root.ns().content22 context = self._new_context(doc)23 title = get_content(context.xpathEval(self._add_ns('/feed/title')))24 description = title25 FeedPlugin.__init__(self, ui_wrap_func, main, url, title, description, doc)26 context.xpathFreeContext()27 def _supports(version):28 if version >= '4.3.0':29 return True30 return False31 supports = staticmethod(_supports)32 def _matches_type(mimetype, subtype):33 if (mimetype == AtomFeed.mimetype and 34 subtype == AtomFeed.subtype ):35 return True36 return False37 matches_type = staticmethod(_matches_type)38 39 def _add_ns(self, xpath_expression):40 return xpath_expression.replace('/', '/%s:' % self.namespace_prefix)41 def _new_context(self, doc=None):42 if doc is None:43 doc = self.doc44 context = doc.xpathNewContext()45 context.xpathRegisterNs(self.namespace_prefix, self.namespace)46 return context47 def get_items(self):48 items = []49 if self.doc is None:50 return items51 context = self._new_context()52 res = context.xpathEval( self._add_ns('/feed/entry') )53 for i in res:54 DISGUSTING_HACK = "*[local-name()='%%s' and namespace-uri()='%s']" % self.namespace55 desc = get_content(i.xpathEval(DISGUSTING_HACK % 'summary'))56 title = get_content(i.xpathEval(DISGUSTING_HACK % 'title'))57 url = get_content(i.xpathEval(DISGUSTING_HACK % 'link' + '/@href'))58 title = title.strip()59 item = (url, title, desc)60 items.append(item)61 ...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!