Best Python code snippet using pyresttest_python
test_generators.py
Source:test_generators.py
...17 self.assertTrue(val is not None)18 if value_test_function:19 self.assertTrue(value_test_function(20 val), 'Test failed with value {0}'.format(val))21 def generator_repeat_test(self, generator_input):22 """ Basic test of a configured generator """23 val = next(generator_input)24 # Check for not repeating easily25 for x in xrange(0, 5):26 val2 = next(generator_input)27 self.assertTrue(val)28 self.assertTrue(val != val2)29 val = val230 def test_factory_ids(self):31 f = generators.factory_generate_ids(1)()32 f2 = generators.factory_generate_ids(101)()33 f3 = generators.factory_generate_ids(1)()34 vals = [next(f), next(f)]35 vals2 = [next(f2), next(f2)]36 vals3 = [next(f3), next(f3)]37 self.assertEqual(1, vals[0])38 self.assertEqual(2, vals[1])39 self.assertEqual(101, vals2[0])40 self.assertEqual(102, vals2[1])41 # Check for accidental closure42 self.assertEqual(1, vals3[0])43 self.assertEqual(2, vals3[1])44 def test_basic_ids(self):45 """ Test starting ids """46 ids1 = generators.generator_basic_ids()47 ids2 = generators.generator_basic_ids()48 self.generator_repeat_test(ids1)49 self.generator_repeat_test(ids2)50 self.assertEqual(next(ids1), next(ids2))51 def test_random_ids(self):52 """ Test random in ids generator """53 gen = generators.generator_random_int32()54 print(next(gen))55 self.generator_repeat_test(gen)56 def test_system_variables(self):57 """ Test generator for binding system variables """58 variable = 'FOOBARBAZ'59 value = 'myTestVal'60 old_val = os.environ.get(variable)61 generator = generators.factory_env_variable(variable)()62 self.assertTrue(next(generator) is None)63 os.environ[variable] = value64 self.assertEqual(value, next(generator))65 self.assertEqual(next(generator), os.path.expandvars('$' + variable))66 # Restore environment67 if old_val is not None:68 os.environ[variable] = old_val69 else:70 del os.environ[variable]71 def test_factory_text(self):72 """ Test the basic generator """73 charsets = [string.ascii_letters, string.digits,74 string.ascii_uppercase, string.hexdigits]75 # Test multiple charsets and string lengths76 for charset in charsets:77 # Test different lengths for charset78 for my_length in xrange(1, 17):79 gen = generators.factory_generate_text(80 legal_characters=charset, min_length=my_length, max_length=my_length)()81 for x in xrange(0, 10):82 val = next(gen)83 self.assertEqual(my_length, len(val))84 def test_factory_sequence(self):85 """ Tests linear sequences """86 vals = [1]87 gen = generators.factory_fixed_sequence(vals)()88 self.generator_basic_test(gen, lambda x: x in vals)89 vals = ['moobie', 'moby', 'moo']90 gen = generators.factory_fixed_sequence(vals)()91 self.generator_basic_test(gen, lambda x: x in vals)92 vals = set(['a', 'b', 'c'])93 gen = generators.factory_fixed_sequence(vals)()94 self.generator_basic_test(gen, lambda x: x in vals)95 def test_parse_fixed_sequence(self):96 vals = ['moobie', 'moby', 'moo']97 config = {'type': 'fixed_sequence',98 'values': vals}99 gen = generators.parse_generator(config)100 self.generator_basic_test(gen, lambda x: x in vals)101 def test_factory_choice(self):102 """ Tests linear sequences """103 vals = [1]104 gen = generators.factory_choice_generator(vals)()105 self.generator_basic_test(gen, lambda x: x in vals)106 vals = ['moobie', 'moby', 'moo']107 gen = generators.factory_choice_generator(vals)()108 self.generator_basic_test(gen, lambda x: x in vals)109 vals = set(['a', 'b', 'c'])110 gen = generators.factory_choice_generator(vals)()111 self.generator_basic_test(gen, lambda x: x in vals)112 def test_parse_choice_generatpr(self):113 vals = ['moobie', 'moby', 'moo']114 config = {'type': 'choice',115 'values': vals}116 gen = generators.parse_generator(config)117 self.generator_basic_test(gen, lambda x: x in vals)118 def test_factory_text_multilength(self):119 """ Test that the random text generator can handle multiple lengths """120 gen = generators.factory_generate_text(121 legal_characters='abcdefghij', min_length=1, max_length=100)()122 lengths = set()123 for x in range(0, 100):124 lengths.add(len(next(gen)))125 self.assertTrue(len(126 lengths) > 1, "Variable length string generator did not generate multiple string lengths")127 def test_character_sets(self):128 """ Verify all charsets are valid """129 sets = generators.CHARACTER_SETS130 for key, value in sets.items():131 self.assertTrue(value)132 def test_parse_text_generator(self):133 """ Test the text generator parsing """134 config = dict()135 config['type'] = 'random_text'136 config['character_set'] = 'reallyINVALID'137 try:138 gen = generators.parse_generator(config)139 self.fail(140 "Should never parse an invalid character_set successfully, but did!")141 except ValueError:142 pass143 # Test for character set handling144 for charset in generators.CHARACTER_SETS:145 try:146 config['character_set'] = charset147 gen = generators.parse_generator(config)148 myset = set(generators.CHARACTER_SETS[charset])149 for x in xrange(0, 50):150 val = next(gen)151 self.assertTrue(set(val).issubset(set(myset)))152 except Exception as e:153 print('Exception occurred with charset: ' + charset)154 raise e155 my_min = 1156 my_max = 10157 # Test for explicit character setting158 del config['character_set']159 temp_chars = 'ay78%&'160 config['characters'] = temp_chars161 gen = generators.parse_generator(config)162 self.generator_basic_test(163 gen, value_test_function=lambda x: set(x).issubset(set(temp_chars)))164 # Test for length setting165 config['length'] = '3'166 gen = generators.parse_generator(config)167 self.generator_basic_test(168 gen, value_test_function=lambda x: len(x) == 3)169 del config['length']170 # Test for explicit min/max length171 config['min_length'] = '9'172 config['max_length'] = 12173 gen = generators.parse_generator(config)174 self.generator_basic_test(175 gen, value_test_function=lambda x: len(x) >= 9 and len(x) <= 12)176 def test_parse_basic(self):177 """ Test basic parsing, simple cases that should succeed or throw known errors """178 config = {'type': 'unsupported'}179 try:180 gen = generators.parse_generator(config)181 self.fail(182 "Expected failure due to invalid generator type, did not emit it")183 except ValueError:184 pass185 # Try creating a random_int generator186 config['type'] = 'random_int'187 gen = generators.parse_generator(config)188 self.generator_basic_test(189 gen, value_test_function=lambda x: isinstance(x, int))190 self.generator_repeat_test(gen)191 # Sample variable192 os.environ['SAMPLEVAR'] = 'goober'193 config['type'] = 'env_variable'194 config['variable_name'] = 'SAMPLEVAR'195 gen = generators.parse_generator(config)196 self.generator_basic_test(gen)197 del config['variable_name']198 config['type'] = 'env_string'199 config['string'] = '$SAMPLEVAR'200 gen = generators.parse_generator(config)201 self.generator_basic_test(gen)202 del config['string']203 config['type'] = 'number_sequence'204 config['start'] = '1'...
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!!