Best Python code snippet using hypothesis
tests.py
Source:tests.py
...22 Increments global variables tests_ran and tests_passed23 """24 results_dict['tests_ran'] += 125 results_dict['tests_passed'] += 126def test_give_me_a_string(results_dict):27 """28 Checks that lab1.give_me_a_string() returns a string type29 """30 print("")31 print("Testing lab1.give_me_a_string()")32 message = "----"33 try:34 result = lab1.give_me_a_string()35 if not isinstance(result, str):36 message += 'Did not return a string - Fail'37 test_failed(results_dict)38 else:39 message += 'A string was returned - Pass'40 test_passed(results_dict)41 except AttributeError:42 message += 'Could not find lab1.give_me_a_string function - Fail'43 test_failed(results_dict)44 print(message)45def test_give_me_an_integer(results_dict):46 """47 Checks that lab1.give_me_an_integer() returns an integer type48 """49 print("")50 print("Testing lab1.give_me_an_integer()")51 message = "----"52 try:53 result = lab1.give_me_an_integer()54 if not isinstance(result, int):55 message += 'Did not return an integer - Fail'56 test_failed(results_dict)57 else:58 message += 'An integer was returned - Pass'59 test_passed(results_dict)60 except AttributeError:61 message += 'Could not find lab1.give_me_an_integer function - Fail'62 test_failed(results_dict)63 print(message)64def test_give_me_a_boolean(results_dict):65 """66 Checks that lab1.give_me_a_boolean() returns a booeal type67 """68 print("")69 print("Testing lab1.give_me_a_boolean()")70 message = "----"71 try:72 result = lab1.give_me_a_boolean()73 if not isinstance(result, bool):74 message += 'Did not return a boolean - Fail'75 test_failed(results_dict)76 else:77 message += 'A boolean was returned - Pass'78 test_passed(results_dict)79 except AttributeError:80 message += 'Could not find lab1.give_me_a_boolean function - Fail'81 test_failed(results_dict)82 print(message)83def test_give_me_a_float(results_dict):84 """85 Checks that lab1.give_me_a_boolean() returns a boolean type86 """87 print("")88 print("Testing lab1.give_me_a_float()")89 message = "----"90 try:91 result = lab1.give_me_a_float()92 if not isinstance(result, float):93 message += 'Did not return a float - Fail'94 test_failed(results_dict)95 else:96 message += 'A float was returned - Pass'97 test_passed(results_dict)98 except AttributeError:99 message += 'Could not find lab1.give_me_a_float function - Fail'100 test_failed(results_dict)101 print(message)102def test_give_me_a_list(results_dict):103 """104 Checks that lab1.give_me_a_list() returns a list type105 """106 print("")107 print("Testing lab1.give_me_a_list()")108 message = "----"109 try:110 result = lab1.give_me_a_list()111 if not isinstance(result, list):112 message += 'Did not return a list - Fail'113 test_failed(results_dict)114 else:115 message += 'A list was returned - Pass'116 test_passed(results_dict)117 except AttributeError:118 message += 'Could not find lab1.give_me_a_list function - Fail'119 test_failed(results_dict)120 print(message)121def test_give_me_a_dictionary(results_dict):122 """123 Checks that lab1.give_me_a_dictionary() returns a dictionary type124 """125 print("")126 print("Testing lab1.give_me_a_dictionary()")127 message = "----"128 try:129 result = lab1.give_me_a_dictionary()130 if not isinstance(result, dict):131 message += 'Did not return a dictionary - Fail'132 test_failed(results_dict)133 else:134 message += 'A dictionary was returned - Pass'135 test_passed(results_dict)136 except AttributeError:137 message += 'Could not find lab1.give_me_a_dictionary function - Fail'138 test_failed(results_dict)139 print(message)140def test_give_me_a_tuple(results_dict):141 """142 Checks that lab1.give_me_a_tuple() returns a tuple type143 """144 print("")145 print("Testing lab1.give_me_a_tuple()")146 message = "----"147 try:148 result = lab1.give_me_a_tuple()149 if not isinstance(result, tuple):150 message += 'Did not return a tuple - Fail'151 test_failed(results_dict)152 else:153 message += 'A tuple was returned - Pass'154 test_passed(results_dict)155 except AttributeError:156 message += 'Could not find lab1.give_me_a_tuple function - Fail'157 test_failed(results_dict)158 print(message)159def test_check_is_even(results_dict):160 """161 Checks that lab1.check_is_even() returns true for even numbers162 """163 print("")164 print("Testing lab1.check_is_even()")165 message = "----"166 try:167 result = lab1.check_is_even(2)168 if not isinstance(result, bool):169 message += 'Did not return a boolean - Fail'170 test_failed(results_dict)171 else:172 message += 'A boolean was returned - Pass'173 test_passed(results_dict)174 # now check if true175 if result is True:176 message += '\n----check_is_even(2) returns True - Pass'177 test_passed(results_dict)178 else:179 message += '\n----check_is_even(2) returns False - Fail'180 test_failed(results_dict)181 # try again, with odd number182 result = lab1.check_is_even(1)183 if result is False:184 message += '\n----check_is_even(1) returns False - Pass'185 test_passed(results_dict)186 else:187 message += '\n----check_is_even(1) returns True - Fail'188 test_failed(results_dict)189 except AttributeError:190 message += 'Could not find lab1.check_is_even function - Fail'191 test_failed(results_dict)192 print(message)193def test_sum_numbers_one_to_ten(results_dict):194 """195 Checks that lab1.sum_numbers_one_to_ten() returns a the correct value196 """197 print("")198 print("Testing lab1.sum_numbers_one_to_ten()")199 message = "----"200 try:201 result = lab1.sum_numbers_one_to_ten()202 if not isinstance(result, int):203 message += 'Did not return an integer - Fail'204 test_failed(results_dict)205 else:206 message += 'An integer was returned - Pass'207 test_passed(results_dict)208 # now check if the value is correct, should be 55209 if result is not 55:210 message += '\n----Did not return the correct number - Fail'211 test_failed(results_dict)212 else:213 message += '\n----The correct value (55) was returned - Pass'214 test_passed(results_dict)215 except AttributeError:216 message += 'Could not find lab1.sum_numbers_one_to_ten function - Fail'217 test_failed(results_dict)218 print(message)219def test_check_is_less_than(results_dict):220 """221 Checks that lab1.check_is_less_than(number1, number2) returns222 True if number1 is less than number2, else False223 """224 print("")225 print("Testing lab1.check_is_less_than()")226 message = "----"227 try:228 result = lab1.check_is_less_than(2, 4)229 if not isinstance(result, bool):230 message += 'Did not return a boolean - Fail'231 test_failed(results_dict)232 else:233 message += 'A boolean was returned - Pass'234 test_passed(results_dict)235 # now check if the result is true (2 is less than 4)236 if result is True:237 message += '\n---- 2 is less than 4, True - Pass'238 test_passed(results_dict)239 else:240 message += '\n---- 2 is less than 4, False - Fail'241 test_failed(results_dict)242 # try again, with number1 > number2243 result = lab1.check_is_less_than(6, 4)244 if result is False:245 message += '\n---- 6 is less than 4, False - Pass'246 test_passed(results_dict)247 else:248 message += '\n---- 6 is less than 4, True - Fail'249 test_failed(results_dict)250 except AttributeError:251 message += 'Could not find lab1.check_is_less_than function - Fail'252 test_failed(results_dict)253 print(message)254def test_sorting(results_dict):255 """256 Checks lab1.sorting(my_list) returns a sorted list257 """258 unsorted_list=[12,7,1,9,15,18,21,3]259 print("")260 print("Testing lab1.sorting()")261 message = "----"262 try:263 result=lab1.sorting(unsorted_list)264 if not isinstance(result, list):265 message+="Did not return a sorted list -Fail"266 test_failed(results_dict)267 #now check if the list is sorted268 else:269 message+="A list was returned - Pass"270 test_passed(results_dict)271 unsorted_list.sort()272 if result==unsorted_list:273 message+='\n----Result is a perfectly sorted list -Pass'274 test_passed(results_dict)275 else:276 message+='\n----Result is not a sorted list -Fail'277 test_failed(results_dict)278 except AttributeError:279 message += 'Could not find lab1.check_is_less_than function - Fail'280 test_failed(results_dict)281 print(message)282def test_perfect_num(results_dict):283 """284 Perfect number is a positive integer that is equal to the sum of all its285 positive divisors except itself286 Example:287 Input: 28288 Output: true289 Explanation: 28=1+2+4+7+14290 """291 print("")292 print("Testing lab1.perfect_num()")293 message = "----"294 try:295 result=lab1.perfect_num(28)296 if not isinstance(result, bool):297 message+="Did not return a boolean value-Fail"298 test_failed(results_dict)299 #now check if the result is true or not300 else:301 message+="A boolean was returned - Pass"302 test_passed(results_dict)303 if result is True:304 message+='\n----(28) is a perfect number -Pass'305 test_passed(results_dict)306 else:307 message+='\n----The returned value is incorrect -Fail'308 test_failed(results_dict)309 except AttributeError:310 message += 'Could not find lab1.check_is_less_than function - Fail'311 test_failed(results_dict)312 print(message)313def run():314 """315 This function is the test runner. It calls the functions316 declared in this file that are used to test the functions317 declared in lab1.py318 """319 results_dict = {320 'tests_ran': 0,321 'tests_passed': 0,322 'tests_failed': 0,323 }324 test_give_me_a_string(results_dict)325 test_give_me_an_integer(results_dict)326 test_give_me_a_boolean(results_dict)327 test_give_me_a_float(results_dict)328 test_give_me_a_list(results_dict)329 test_give_me_a_dictionary(results_dict)330 test_give_me_a_tuple(results_dict)331 test_sum_numbers_one_to_ten(results_dict)332 test_check_is_even(results_dict)333 test_check_is_less_than(results_dict)334 test_sorting(results_dict)335 test_perfect_num(results_dict)336 print("")337 print("Final Results:")338 results = "%s tests ran: %s passed - %s failed." % (...
test_setup_teardown.py
Source:test_setup_teardown.py
...31 @given(integers())32 def give_me_an_int(self, x):33 pass34 @given(text())35 def give_me_a_string(myself, x):36 pass37 @given(integers())38 def give_me_a_positive_int(self, x):39 assert x >= 040 @given(integers().map(lambda x: x.nope))41 def fail_in_reify(self, x):42 pass43 @given(integers())44 def assume_some_stuff(self, x):45 assume(x > 0)46 @given(integers().filter(lambda x: x > 0))47 def assume_in_reify(self, x):48 pass49class HasSetupAndTeardown(HasSetup, HasTeardown, SomeGivens):50 pass51def test_calls_setup_and_teardown_on_self_as_first_argument():52 x = HasSetupAndTeardown()53 x.give_me_an_int()54 x.give_me_a_string()55 assert x.setups > 056 assert x.teardowns == x.setups57def test_calls_setup_and_teardown_on_self_unbound():58 x = HasSetupAndTeardown()59 HasSetupAndTeardown.give_me_an_int(x)60 assert x.setups > 061 assert x.teardowns == x.setups62def test_calls_setup_and_teardown_on_failure():63 x = HasSetupAndTeardown()64 with pytest.raises(AssertionError):65 x.give_me_a_positive_int()66 assert x.setups > 067 assert x.teardowns == x.setups68def test_still_tears_down_on_failed_reify():...
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!!