Best Python code snippet using tempest_python
argument_handling.py
Source:argument_handling.py
...16 args.add_opt(self.mod.OPT_TYPE_FLAG, 'b', name, 'A boolean flag.')17 args.process([])18 self.assertFalse(args[name])19 self.assertFalse(name in args)20 self.assertEmpty(self.errors)21 self.assertEmpty(self.lines)22 def test_bool_flag_true(self):23 for name in ['bool', 'other-value']:24 args = self.mod.ArgHelper()25 args.add_opt(self.mod.OPT_TYPE_FLAG, 'b', name, 'A boolean flag.')26 args.process(['-b'])27 self.assertTrue(args[name])28 self.assertTrue(name in args)29 self.assertEmpty(self.errors)30 self.assertEmpty(self.lines)31 def test_bool_longflag_false(self):32 for flag in ['long', 'another']:33 for name in ['bool', 'other-value']:34 args = self.mod.ArgHelper()35 args.add_opt(self.mod.OPT_TYPE_LONG_FLAG, 'b', name, 'A boolean flag.')36 args.process([])37 self.assertFalse(args[name])38 self.assertEmpty(self.errors)39 self.assertEmpty(self.lines)40 def test_bool_longflag_true(self):41 for flag in ['long', 'another']:42 for name in ['bool', 'other-value']:43 args = self.mod.ArgHelper()44 args.add_opt(self.mod.OPT_TYPE_LONG_FLAG, flag, name, 'A boolean flag.')45 args.process(['--' + flag])46 self.assertTrue(args[name])47 self.assertEmpty(self.errors)48 self.assertEmpty(self.lines)49 def test_int_value_conversion(self):50 for label in ['int-value', 'another-value']:51 for i in ['nope', 'not-a-number']:52 self.errors = []53 args = self.mod.ArgHelper()54 args.add_opt(self.mod.OPT_TYPE_SHORT, 'a', label, 'An example integer that takes an argument.', converter=int, default = 0)55 with self.assertRaises(SystemExit) as ex:56 args.process(['-a', i])57 self.assertEqual(ex.exception.code, 1)58 e = self.assertSingle(self.errors)59 self.assertEqual('Unable to convert %s to int: %s' % (label, i), e)60 self.assertEmpty(self.lines)61 def test_error_single(self):62 for label in ['string-single', 'other']:63 self.errors = []64 args = self.mod.ArgHelper()65 args.add_opt(self.mod.OPT_TYPE_SHORT, 's', label, 'A short arg that insists on a single argument.', strict_single = True)66 with self.assertRaises(SystemExit) as ex:67 args.process(['-s', 'a', '-s', 'b'])68 self.assertEqual(ex.exception.code, 1)69 e = self.assertSingle(self.errors)70 self.assertEqual('Cannot have multiple %s values.' % label, e)71 self.assertEmpty(self.lines)72 def test_help_exit(self):73 for code in [0,1,2]:74 args = self.mod.ArgHelper()75 with self.assertRaises(SystemExit) as ex:76 args.hexit(code)77 self.assertEqual(code, ex.exception.code)78 self.assertStartsWith('[Usage] ', self.lines[0])79 self.assertEndsWith('[-h]', self.lines[0])80 self.assertEqual('-h: Display a help menu and then exit.', self.lines[-1])81 def test_help_exit_default(self):82 args = self.mod.ArgHelper()83 with self.assertRaises(SystemExit) as ex:84 args.hexit()85 self.assertEqual(0, ex.exception.code)86 self.assertStartsWith('[Usage] ', self.lines[0])87 self.assertEndsWith('[-h]', self.lines[0])88 self.assertEqual('-h: Display a help menu and then exit.', self.lines[-1])89 def test_help_no_exit(self):90 args = self.mod.ArgHelper()91 args.hexit(-1)92 self.assertStartsWith('[Usage] ', self.lines[0])93 self.assertEndsWith('[-h]', self.lines[0])94 self.assertEqual('-h: Display a help menu and then exit.', self.lines[-1])95 def test_int_value_multi(self):96 for case in [97 [1,2,3,4],98 [2],99 [5,6,7],100 [1,1,2,2]101 ]:102 opts = []103 for c in case:104 opts.extend(['-i', str(c)])105 self.assertEqual(len(case) * 2, len(opts))106 args = self.mod.ArgHelper()107 args.add_opt(self.mod.OPT_TYPE_SHORT, 'i', 'int-multiple', 'An example integer that takes multiple values.', converter=int, multiple = True)108 args.process(opts)109 value = args['int-multiple']110 self.assertTrue(type(value) is list)111 self.assertEqual(len(case), len(value))112 for c in case:113 self.assertContains(c, value)114 self.assertEqual(case, value)115 def test_int_value_multi_empty(self):116 args = self.mod.ArgHelper()117 args.add_opt(self.mod.OPT_TYPE_SHORT, 'i', 'int-multiple', 'An example integer that takes multiple values.', converter=int, multiple = True)118 args.process([])119 value = args['int-multiple']120 self.assertTrue(type(value) is list)121 self.assertEmpty(value)122 def test_int_value_single(self):123 args = self.mod.ArgHelper()124 args.add_opt(self.mod.OPT_TYPE_SHORT, 'a', 'int-value', 'An example integer that takes an argument.', converter=int, default = 0)125 for i in [5, 2, 1, -2]:126 args.process(['-a', str(i)])127 self.assertEqual(i, args['int-value'])128 self.assertEmpty(self.errors)129 self.assertEmpty(self.lines)130 def test_int_value_single_default(self):131 for d in [0,2,3,4]:132 args = self.mod.ArgHelper()133 args.add_opt(self.mod.OPT_TYPE_SHORT, 'a', 'int-value', 'An example integer that takes an argument.', converter=int, default = d)134 args.process([])135 self.assertEqual(d, args['int-value'])136 self.assertEmpty(self.errors)137 self.assertEmpty(self.lines)138 def test_int_value_overwrite(self):139 args = self.mod.ArgHelper()140 args.add_opt(self.mod.OPT_TYPE_SHORT, 'a', 'int-value', 'An example integer that takes an argument.', converter=int, default = 0)141 for i in [5, 2, 1, -2]:142 args.process(['-a', '0', '-a', str(i)])143 self.assertEqual(i, args['int-value'])144 self.assertEmpty(self.errors)145 self.assertEmpty(self.lines)146 def test_int_value_single_nodefault(self):147 args = self.mod.ArgHelper()148 args.add_opt(self.mod.OPT_TYPE_SHORT, 'a', 'int-value', 'An example integer that takes an argument.', converter=int)149 args.process([])150 self.assertNone(args['int-value'])151 self.assertEmpty(self.errors)152 self.assertEmpty(self.lines)153 def test_string_plus_operands(self):154 for operands in [['aaa','bbb','ccc']]:155 for value in ['a','b','c','d']:156 for flag in ['a','b','c','d']:157 for label in ['a','b','c','d']:158 args = self.mod.ArgHelper()159 args.add_opt(self.mod.OPT_TYPE_SHORT, flag, label, 'A string value.')160 args.process(operands + ['-' + flag, value])161 self.assertEqual(value, args[label])162 self.assertEqual(operands, args.operands)163 self.assertEqual(operands[-1], args.last_operand('DEFAULT'))164 self.assertEmpty(self.errors)165 self.assertEmpty(self.lines)166 def test_string_plus_operands_default(self):167 for default in ['DEFAULT', 'another-default']:168 for value in ['a','b','c','d']:169 for flag in ['a','b','c','d']:170 for label in ['a','b','c','d']:171 args = self.mod.ArgHelper()172 args.add_opt(self.mod.OPT_TYPE_SHORT, flag, label, 'A string value.')173 args.process(['-' + flag, value])174 self.assertEqual(value, args[label])175 self.assertEqual(default, args.last_operand(default))176 self.assertEmpty(self.errors)177 self.assertEmpty(self.lines)178 def test_string(self):179 for value in ['a','b','c','d']:180 for flag in ['a','b','c','d']:181 for label in ['a','b','c','d']:182 args = self.mod.ArgHelper()183 args.add_opt(self.mod.OPT_TYPE_SHORT, flag, label, 'A string value.')184 args.process(['-' + flag, value])185 self.assertEqual(value, args[label])186 self.assertEmpty(self.errors)187 self.assertEmpty(self.lines)188 def test_string_long(self):189 for value in ['a','b','c','d']:190 for flag in ['a','b','c','d','aa','bb','cc','dd']:191 for label in ['a','b','c','d']:192 args = self.mod.ArgHelper()193 args.add_opt(self.mod.OPT_TYPE_LONG, flag, label, 'A string value.')194 args.process(['--' + flag, value])195 self.assertEqual(value, args[label])196 self.assertEmpty(self.errors)...
test_id_title_collector_file_decorator.py
Source:test_id_title_collector_file_decorator.py
...11 'files': {},12 'subnodes': [],13 }14 state, decorator = self.decorate(node)15 self.assertEmpty(state['id-title-map'])16 self.assertEmpty(decorator.get_messages(state))17 def test_plain(self):18 node = {19 'name': 'bar',20 'files': {21 'en': {22 'id': 'id-foo',23 'key': 'en',24 'markdown': 'foo\nbar',25 },26 },27 'subnodes': [],28 }29 state, decorator = self.decorate(node)30 self.assertEqual(state['id-title-map']['id-foo']['en'], 'foo')31 self.assertLenIs(state['id-title-map']['id-foo'], 1)32 self.assertEmpty(decorator.get_messages(state))33 def test_multiple_files(self):34 node = {35 'name': 'bar',36 'files': {37 'en': {38 'id': 'id-foo',39 'key': 'en',40 'markdown': 'foo\nbar',41 },42 'de': {43 'id': 'id-foo',44 'key': 'de',45 'markdown': 'quux\nbar',46 },47 },48 'subnodes': [],49 }50 state, decorator = self.decorate(node)51 self.assertEqual(state['id-title-map']['id-foo']['en'], 'foo')52 self.assertEqual(state['id-title-map']['id-foo']['de'], 'quux')53 self.assertLenIs(state['id-title-map']['id-foo'], 2)54 self.assertEmpty(decorator.get_messages(state))55 def test_empty_lines(self):56 node = {57 'name': 'bar',58 'files': {59 'en': {60 'id': 'id-foo',61 'key': 'en',62 'markdown': '\n \nfoo\nbar',63 },64 },65 'subnodes': [],66 }67 state, decorator = self.decorate(node)68 self.assertEqual(state['id-title-map']['id-foo']['en'], 'foo')69 self.assertLenIs(state['id-title-map']['id-foo'], 1)70 self.assertEmpty(decorator.get_messages(state))71 def test_section_marker_stripping(self):72 node = {73 'name': 'bar',74 'files': {75 'en': {76 'id': 'id-foo',77 'key': 'en',78 'markdown': '## # # foo\nbar',79 },80 },81 'subnodes': [],82 }83 state, decorator = self.decorate(node)84 self.assertEqual(state['id-title-map']['id-foo'],85 {'en': 'foo'})86 self.assertEmpty(decorator.get_messages(state))87 def test_attribute_stripping(self):88 node = {89 'name': 'bar',90 'files': {91 'en': {92 'id': 'id-foo',93 'key': 'en',94 'markdown': 'foo {=BAR} {: class=quux}\nbar',95 },96 },97 'subnodes': [],98 }99 state, decorator = self.decorate(node)100 self.assertEqual(state['id-title-map']['id-foo'],101 {'en': 'foo {=BAR}'})102 self.assertEmpty(decorator.get_messages(state))103 def test_anonymous(self):104 node = {105 'name': 'bar',106 'files': {107 'en': {108 'id': 'id-foo',109 'key': 'en',110 'markdown': '',111 },112 },113 'subnodes': [],114 }115 state, decorator = self.decorate(node)116 self.assertEqual(state['id-title-map']['id-foo'],117 {'en': '(anonymous)'})118 messages = decorator.get_messages(state)119 self.assertEqual(messages[0]['kind'], 'error')120 self.assertIn('title', messages[0]['text'])121 self.assertLenIs(messages, 1)122 def test_default(self):123 node = {124 'name': 'bar',125 'files': {126 'default': {127 'id': 'id-foo',128 'key': 'en',129 'is-default': True,130 'markdown': 'foo'131 },132 },133 'subnodes': [],134 }135 state, decorator = self.decorate(node)136 self.assertEqual(state['id-title-map']['id-foo'],137 {'default': 'foo'})...
dot_test.py
Source:dot_test.py
...25import jax.numpy as jnp26class DotTest(parameterized.TestCase):27 def test_empty(self):28 graph, args, out = dot.to_graph(lambda: None)()29 self.assertEmpty(args)30 self.assertIsNone(out)31 self.assertEmpty(graph.nodes)32 self.assertEmpty(graph.edges)33 self.assertEmpty(graph.subgraphs)34 @test_utils.transform_and_run35 def test_add_module(self):36 mod = AddModule()37 a = b = jnp.ones([])38 graph, args, c = dot.to_graph(mod)(a, b)39 self.assertEqual(args, (a, b))40 self.assertEqual(c, a + b)41 self.assertEmpty(graph.edges)42 add_graph, = graph.subgraphs43 self.assertEqual(add_graph.title, "add_module")44 self.assertEmpty(add_graph.subgraphs)45 add_edge_a, add_edge_b = add_graph.edges46 self.assertEqual(add_edge_a, (a, c))47 self.assertEqual(add_edge_b, (b, c))48 add_node, = add_graph.nodes49 self.assertEqual(add_node.title, "add")50 add_out, = add_node.outputs51 self.assertEqual(add_out, c)52 @test_utils.transform_and_run53 def test_inline_jit_add_module(self):54 mod = InlineJitAddModule()55 a = b = jnp.ones([])56 graph, args, c = dot.to_graph(mod)(a, b)57 self.assertEqual(args, (a, b))58 self.assertEqual(c, a + b)59 self.assertEmpty(graph.edges)60 add_graph, = graph.subgraphs61 self.assertEqual(add_graph.title, "inline_jit_add_module")62 self.assertEmpty(add_graph.subgraphs)63 add_edge_a, add_edge_b = add_graph.edges64 self.assertEqual(add_edge_a, (a, c))65 self.assertEqual(add_edge_b, (b, c))66 add_node, = add_graph.nodes67 self.assertEqual(add_node.title, "add")68 add_out, = add_node.outputs69 self.assertEqual(add_out, c)70 @test_utils.transform_and_run71 def test_no_namescopes_inside_abstract_dot(self):72 mod = AddModule()73 a = b = jax.ShapeDtypeStruct(shape=tuple(), dtype=jnp.float32)74 with config.context(profiler_name_scopes=True):75 with mock.patch.object(stateful, "named_call") as mock_f:76 _ = dot.abstract_to_dot(mod)(a, b)77 mock_f.assert_not_called()78 def test_call(self):79 def my_function(x):80 return x81 graph, _, _ = dot.to_graph(jax.jit(my_function))(jnp.ones([]))82 self.assertEmpty(graph.nodes)83 self.assertEmpty(graph.edges)84 jit, = graph.subgraphs85 self.assertEqual(jit.title, "xla_call (my_function)")86 def test_pmap(self):87 def my_function(x):88 return x89 n = jax.local_device_count()90 graph, _, _ = dot.to_graph(jax.pmap(my_function))(jnp.ones([n]))91 self.assertEmpty(graph.nodes)92 self.assertEmpty(graph.edges)93 jit, = graph.subgraphs94 self.assertEqual(jit.title, "xla_pmap (my_function)")95 @test_utils.transform_and_run96 def test_no_namescopes_inside_dot(self):97 mod = AddModule()98 with config.context(profiler_name_scopes=True):99 with mock.patch.object(stateful, "named_call") as mock_f:100 _ = dot.to_dot(mod)(1, 1)101 mock_f.assert_not_called()102 @parameterized.parameters({True, False})103 def test_module_namescope_setting_unchanged(self, flag):104 with config.context(profiler_name_scopes=flag):105 _ = dot.to_dot(lambda x: x)(jnp.ones((1, 1)))106 self.assertEqual(config.get_config().profiler_name_scopes, flag)...
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!!