Best Python code snippet using Testify_python
test_dunderdecorators.py
Source:test_dunderdecorators.py
1from dunderdecorators import dunder_iter, dunder_setitem, \2 dunder_getitem, dunder_missing, dunder_repr, \3 DunderDecoratorException4import pytest5from typing import List, Dict, Set6def test_dunder_iter():7 @dunder_iter8 class Test(object):9 def __init__(10 self,11 a: int,12 b: int,13 c: int,14 ) -> None:15 self.a = a16 self.b = b17 self.c = c18 test = Test(1, 2, 3)19 test_values = [i for i in test]20 assert test_values == [21 ('a', 1), 22 ('b', 2), 23 ('c', 3)24 ]25def test_dunder_iter_with_non_mapping_attr():26 @dunder_iter(attr='a')27 class Test(object):28 def __init__(29 self,30 a: List,31 ) -> None:32 self.a = a33 test = Test([1, 2, 3])34 test_values = [i for i in test]35 assert test_values == [1, 2, 3]36def test_dunder_iter_with_mapping_attr():37 @dunder_iter(attr='a')38 class Test(object):39 def __init__(40 self,41 a: Dict,42 ) -> None:43 self.a = a44 test = Test({45 'a' : 1, 46 'b' : 2, 47 'c' : 348 })49 test_values = {k : v for k, v in test}50 assert test_values == {51 'a' : 1, 52 'b' : 2, 53 'c' : 354 }55def test_dunder_iter_slots_exception():56 @dunder_iter(slots=True)57 class Test(object):58 def __init__(59 self,60 a: int,61 b: int,62 c: int,63 ) -> None:64 self.a = a65 self.b = b66 self.c = c67 test = Test(1, 2, 3)68 with pytest.raises(DunderDecoratorException) as exception_info:69 test_values = [i for i in test]70 assert exception_info.value.message == ('slots', 'iter')71def test_dunder_iter_iterable_attr_exception():72 @dunder_iter(attr='a')73 class Test(object):74 def __init__(75 self,76 a: int,77 b: int,78 c: int,79 ) -> None:80 self.a = a81 self.b = b82 self.c = c83 test = Test(1, 2, 3)84 with pytest.raises(DunderDecoratorException) as exception_info:85 test_values = [i for i in test]86 assert exception_info.value.message == ('iterable')87def test_dunder_iter_with_slots():88 @dunder_iter(slots=True)89 class Test(object):90 __slots__ = ('a', 'b', 'c')91 def __init__(92 self,93 a: int,94 b: int,95 c: int,96 ) -> None:97 self.a = a98 self.b = b99 self.c = c100 test = Test(1, 2, 3)101 test_values = [i for i in test]102 assert test_values == [103 ('a', 1), 104 ('b', 2), 105 ('c', 3)106 ]107def test_dunder_iter_with_slots_and_attr():108 @dunder_iter(attr='a')109 class Test(object):110 __slots__ = ('a',)111 def __init__(112 self,113 a: List,114 ) -> None:115 self.a = a116 test = Test([1, 2, 3])117 test_values = [i for i in test]118 assert test_values == [1, 2, 3]119def test_dunder_iter_with_slots_dict_exception():120 @dunder_iter121 class Test(object):122 __slots__ = ('a', 'b', 'c')123 def __init__(124 self,125 a: int,126 b: int,127 c: int,128 ) -> None:129 self.a = a130 self.b = b131 self.c = c132 test = Test(1, 2, 3)133 with pytest.raises(DunderDecoratorException) as exception_info:134 test_values = [i for i in test]135 assert exception_info.value.message == ('dict', 'iter')136def test_dunder_iter_with_slots_iterable_attr_exception():137 @dunder_iter(slots=True, attr='a')138 class Test(object):139 __slots__ = ('a', 'b', 'c')140 def __init__(141 self,142 a: int,143 b: int,144 c: int,145 ) -> None:146 self.a = a147 self.b = b148 self.c = c149def test_dunder_iter_with_slots_and_iter_slots_declaration():150 @dunder_iter(attr='a')151 class Test(object):152 __slots__ = ('a',)153 def __init__(154 self,155 a: List,156 ) -> None:157 self.a = a158 test = Test([1, 2, 3])159 test_values = [i for i in test]160 assert test_values == [1, 2, 3]161def test_dunder_setitem():162 @dunder_setitem163 class Test(object):164 def __init__(165 self,166 a: int,167 b: int,168 c: int,169 ) -> None:170 self.a = a171 self.b = b172 self.c = c173 test = Test(1, 2, 3)174 test['a'] = 3175 test['b'] = 2176 test['c'] = 1177 assert (178 [test.a, test.b, test.c] == 179 [3, 2, 1]180 )181def test_dunder_setitem_with_non_mapping_attr():182 @dunder_setitem(attr='a')183 class Test(object):184 def __init__(185 self,186 a: List,187 ) -> None:188 self.a = a189 test = Test([1, 2, 3])190 test[0] = 3191 test[1] = 2192 test[2] = 1193 assert (194 test.a == 195 [3, 2, 1]196 )197def test_dunder_setitem_with_attr_set():198 @dunder_setitem(attr='a')199 class Test(object):200 def __init__(201 self,202 a: Set,203 ) -> None:204 self.a = a205 with pytest.raises(DunderDecoratorException) as exception_info:206 test = Test(set([1, 2, 3]))207 test[0] = 5208 assert exception_info.value.message == 'indexable'209def test_dunder_getitem_with_attr_set():210 @dunder_getitem(attr='a')211 class Test(object):212 def __init__(213 self,214 a: Set,215 ) -> None:216 self.a = a217 with pytest.raises(DunderDecoratorException) as exception_info:218 test = Test(set([1, 2, 3]))219 print(test[0])220 assert exception_info.value.message == 'indexable'221def test_dunder_iter_with_mapping_attr():222 @dunder_setitem(attr='a')223 class Test(object):224 def __init__(225 self,226 a: Dict,227 ) -> None:228 self.a = a229 test = Test({230 'a' : 1, 231 'b' : 2, 232 'c' : 3233 })234 test['a'] = 3235 test['b'] = 2236 test['c'] = 1237 assert test.a == {238 'a' : 3, 239 'b' : 2, 240 'c' : 1241 }242def test_dunder_setitem_with_slots():243 @dunder_setitem(slots=True)244 class Test(object):245 __slots__ = ('a', 'b', 'c')246 def __init__(247 self,248 a: int,249 b: int,250 c: int,251 ) -> None:252 self.a = a253 self.b = b254 self.c = c255 test = Test(1, 2, 3)256 test['a'] = 3257 test['b'] = 2258 test['c'] = 1259 assert (260 [test.a, test.b, test.c] == 261 [3, 2, 1]262 )263def test_dunder_setitem_with_slots_and_non_mapping_attr():264 @dunder_setitem(attr='a', slots=True)265 class Test(object):266 def __init__(267 self,268 a: List,269 ) -> None:270 self.a = a271 test = Test([1, 2, 3])272 test[0] = 3273 test[1] = 2274 test[2] = 1275 assert (276 test.a == 277 [3, 2, 1]278 )279def test_dunder_setitem_with_slots_and_mapping_attr():280 @dunder_setitem(attr='a', slots=True)281 class Test(object):282 def __init__(283 self,284 a: Dict,285 ) -> None:286 self.a = a287 test = Test({288 'a' : 1, 289 'b' : 2, 290 'c' : 3291 })292 test['a'] = 3293 test['b'] = 2294 test['c'] = 1295 assert test.a == {296 'a' : 3, 297 'b' : 2, 298 'c' : 1299 }300def test_dunder_setitem_indexing_exception():301 @dunder_setitem(attr='a')302 class Test(object):303 def __init__(304 self,305 a: int,306 ) -> None:307 self.a = a308 test = Test(1)309 exception_key_words = [310 'Attribute',311 'belonging',312 'indexable',313 'attr',314 'name',315 'owned',316 'without',317 'parameters',318 '__dict__',319 ]320 exception_null_key_words = [321 'list',322 'provided', 323 'slots', 324 ]325 with pytest.raises(DunderDecoratorException) as exception_info:326 test[0] = 3327 assert exception_info.value.message == 'indexable'328 for key_word in exception_key_words:329 assert key_word in str(exception_info.value)330 for key_word in exception_null_key_words:331 assert key_word not in str(exception_info.value)332def test_dunder_setitem_indexing_has_iterable_attr_exception():333 @dunder_setitem(attr='a')334 class Test(object):335 def __init__(336 self,337 a: int,338 b: List,339 ) -> None:340 self.a = a341 self.b = b342 test = Test(1, [1, 2, 3])343 exception_key_words = [344 'Attribute',345 'belonging',346 'indexable',347 'attr',348 'name',349 'owned',350 'without',351 'parameters',352 '__dict__',353 'list',354 'provided',355 ]356 exception_null_key_words = [357 'slots', 358 ]359 with pytest.raises(DunderDecoratorException) as exception_info:360 test[0] = 3361 assert exception_info.value.message == 'indexable'362 for key_word in exception_key_words:363 assert key_word in str(exception_info.value)364 365def test_dunder_setitem_indexing_with_slots_exception():366 @dunder_setitem(attr='a')367 class Test(object):368 __slots__ = ('a',)369 def __init__(370 self,371 a: int,372 ) -> None:373 self.a = a374 test = Test(1)375 exception_key_words = [376 'Attribute',377 'belonging',378 'indexable',379 'attr',380 'name',381 'owned',382 'slots', 383 'True',384 ]385 exception_null_key_words = [386 'without',387 'parameters',388 '__dict__',389 'list',390 'provided', 391 ]392 with pytest.raises(DunderDecoratorException) as exception_info:393 test[0] = 3394 assert exception_info.value.message == 'indexable'395 for key_word in exception_key_words:396 assert key_word in str(exception_info.value)397 for key_word in exception_null_key_words:398 assert key_word not in str(exception_info.value)399def test_dunder_setitem_indexing_with_slots_and_iterable_attr_exception():400 @dunder_setitem(attr='a')401 class Test(object):402 __slots__ = ('a', 'b', 'c',)403 def __init__(404 self,405 a: int,406 b: List,407 c: List,408 ) -> None:409 self.a = a410 self.b = b411 self.c = c412 test = Test(1, [1, 2, 3], [4, 5, 6])413 exception_key_words = [414 'Attribute',415 'belonging',416 'indexable',417 'attr',418 'name',419 'owned',420 'slots',421 'list',422 'provided',423 '[\'b\', \'c\']',424 ]425 exception_null_key_words = [426 'parameters',427 '__dict__', 428 'without',429 ]430 with pytest.raises(DunderDecoratorException) as exception_info:431 test[0] = 3432 assert exception_info.value.message == 'indexable'433 for key_word in exception_key_words:434 assert key_word in str(exception_info.value)435 for key_word in exception_null_key_words:436 assert key_word not in str(exception_info.value)437def test_dunder_key_not_hashable_exception():438 @dunder_setitem(attr='a')439 class Test(object):440 def __init__(441 self,442 a: Dict,443 ) -> None:444 self.a = a445 test = Test({'a' : 1})446 exception_key_words = [447 'Provided',448 'is',449 'of',450 'type',451 'not',452 'hashable',453 ]454 with pytest.raises(DunderDecoratorException) as exception_info:455 test[[1, 2, 3]] = 3456 assert exception_info.value.message == 'key_not_hashable'457 for key_word in exception_key_words:458 assert key_word in str(exception_info.value)459def test_dunder_setitem_dict_exception():460 @dunder_setitem461 class Test(object):462 __slots__ = ('a',)463 def __init__(464 self,465 a: Dict,466 ) -> None:467 self.a = a468 test = Test({'a' : 1})469 exception_key_words = [470 'has',471 'no',472 'attribute',473 '__dict__',474 'Consider',475 'slots',476 'True',477 ]478 with pytest.raises(DunderDecoratorException) as exception_info:479 test['a'] = [1, 2, 3]480 assert exception_info.value.message == ('dict', 'setitem')481 for key_word in exception_key_words:482 assert key_word in str(exception_info.value)483def test_dunder_setitem_slots_exception():484 @dunder_setitem(slots=True)485 class Test(object):486 def __init__(487 self,488 a: Dict,489 ) -> None:490 self.a = a491 test = Test({'a' : 1})492 exception_key_words = [493 'parameters',494 'Consider',495 '__slots__',496 ]497 with pytest.raises(DunderDecoratorException) as exception_info:498 test['a'] = [1, 2, 3]499 assert exception_info.value.message == ('slots', 'setitem')500 for key_word in exception_key_words:501 assert key_word in str(exception_info.value)502def test_dunder_getitem_dict_exception():503 @dunder_getitem504 class Test(object):505 __slots__ = ('a',)506 def __init__(507 self,508 a: Dict,509 ) -> None:510 self.a = a511 test = Test({'a' : 1})512 exception_key_words = [513 'has',514 'no',515 'attribute',516 'Consider',517 '__dict__',518 ]519 with pytest.raises(DunderDecoratorException) as exception_info:520 x = test['a']521 assert exception_info.value.message == ('dict', 'getitem')522 for key_word in exception_key_words:523 assert key_word in str(exception_info.value)524def test_dunder_getitem():525 @dunder_getitem526 class Test(object):527 def __init__(528 self,529 a: int,530 b: int,531 c: int,532 ) -> None:533 self.a = a534 self.b = b535 self.c = c536 test = Test(1, 2, 3)537 assert (538 [test['a'], test['b'], test['c']] ==539 [1, 2, 3]540 )541def test_dunder_getitem_slots():542 @dunder_getitem(slots=True)543 class Test(object):544 __slots__ = ('a', 'b', 'c')545 def __init__(546 self,547 a: int,548 b: int,549 c: int,550 ) -> None:551 self.a = a552 self.b = b553 self.c = c554 test = Test(1, 2, 3)555 assert (556 [test['a'], test['b'], test['c']] ==557 [1, 2, 3]558 )559def test_dunder_getitem_with_attr():560 @dunder_getitem(attr='a')561 class Test(object):562 def __init__(563 self,564 a: List,565 ) -> None:566 self.a = a567 test = Test([1, 2, 3])568 assert (569 [test[0], test[1], test[2]] ==570 [1, 2, 3]571 )572def test_dunder_setitem_with_iterable_attr():573 @dunder_getitem(attr='a')574 @dunder_setitem(attr='a')575 class Test(object):576 def __init__(577 self,578 a: List,579 ) -> None:580 self.a = a581 test = Test([1, 2, 3])582 test[0] = 3583 test[1] = 2584 test[2] = 1585 assert (586 [test[0], test[1], test[2]] ==587 [3, 2, 1]588 )589def test_dunder_setitem_with_iterable_attr_append():590 @dunder_setitem(attr='a')591 @dunder_getitem(attr='a')592 class Test(object):593 def __init__(594 self,595 a: List,596 ) -> None:597 self.a = a598 test = Test([1, 2, 3])599 test[0] = 3600 test[1] = 2601 test[2] = 1602 test[3] = 0603 assert (604 [test[0], test[1], test[2], test[3]] ==605 [3, 2, 1, 0]606 )607def test_dunder_setitem_with_iterable_attr_append_left():608 @dunder_setitem(attr='a')609 @dunder_getitem(attr='a')610 class Test(object):611 def __init__(612 self,613 a: List,614 ) -> None:615 self.a = a616 test = Test([1, 2, 3])617 test[0] = 3618 test[1] = 2619 test[2] = 1620 test[-6] = 6621 assert (622 [test[0], test[1], test[2], test[3]] ==623 [6, 3, 2, 1]624 )625def test_dunder_setitem_with_iterable_attr_append_left_negative():626 @dunder_setitem(attr='a')627 @dunder_getitem(attr='a')628 class Test(object):629 def __init__(630 self,631 a: List,632 ) -> None:633 self.a = a634 test = Test([1, 2, 3])635 test[-6] = 6636 test[0] = 3637 test[1] = 2638 test[2] = 1639 assert (640 [test[0], test[1], test[2], test[3]] ==641 [3, 2, 1, 3]642 )643def test_dunder_getitem_slots():644 @dunder_getitem(slots=True)645 class Test(object):646 __slots__ = ('a', 'b', 'c')647 def __init__(648 self,649 a: int,650 b: int,651 c: int,652 ) -> None:653 self.a = a654 self.b = b655 self.c = c656 test = Test(1, 2, 3)657 assert (658 [test['a'], test['b'], test['c']] ==659 [1, 2, 3]660 )661 662def test_dunder_missing():663 @dunder_getitem664 @dunder_missing(default_value=1.0)665 class Test(object):666 def __init__(667 self,668 a: int,669 b: int,670 c: int,671 ) -> None:672 self.a = a673 self.b = b674 self.c = c675 test = Test(1, 2, 3)676 test['d']...
daves_hamming_test.py
Source:daves_hamming_test.py
1import pytest2import hamming3# basic functionality4def test_same_singles_means_distance_zero():5 assert hamming.distance("G", "G") == 06def test_different_singles_means_distance_one():7 assert hamming.distance("G", "C") == 18# not much value in testing twos9def test_same_three_means_distance_zero():10 assert hamming.distance("GCA", "GCA") == 011def test_three_different_at_start_means_distance_one():12 assert hamming.distance("GCA", "CCA") == 113def test_three_different_in_middle_means_distance_one():14 assert hamming.distance("GCA", "GGA") == 115def test_three_different_at_end_means_distance_one():16 assert hamming.distance("GCA", "GCG") == 117def test_three_same_only_at_start_means_distance_two():18 assert hamming.distance("GCA", "GTG") == 219def test_three_same_only_in_middle_means_distance_two():20 assert hamming.distance("GCA", "TCC") == 221def test_three_same_only_at_end_means_distance_two():22 assert hamming.distance("GCA", "CGA") == 223def test_different_three_means_distance_three():24 assert hamming.distance("GCA", "CAG") == 325# going up to three should be enough to prove the general case26# light bulletproofing: valid edge case27def test_two_empty_means_distance_zero():28 assert hamming.distance("", "") == 029# medium bulletproofing: invalid edge cases and likely errors30def test_raises_with_msg_if_first_shorter():31 with pytest.raises(ValueError) as exception_info:32 hamming.distance("GC", "TGA")33 assert hamming.LengthMismatchMessage in str(exception_info)34def test_raises_with_msg_if_second_shorter():35 with pytest.raises(ValueError) as exception_info:36 hamming.distance("GCA", "TG")37 assert hamming.LengthMismatchMessage in str(exception_info)38def test_raises_with_msg_if_first_has_invalid_base():39 with pytest.raises(ValueError) as exception_info:40 hamming.distance("CAGE", "CAGT")41 assert hamming.InvalidBaseMessage in str(exception_info)42def test_raises_with_msg_if_second_has_invalid_base():43 with pytest.raises(ValueError) as exception_info:44 hamming.distance("CAGT", "CAGE")45 assert hamming.InvalidBaseMessage in str(exception_info)46# heavy bulletproofing: invalid cases not likely from innocent error47def test_raises_with_msg_if_first_not_string():48 with pytest.raises(ValueError) as exception_info:49 hamming.distance(3, "TG")50 assert hamming.NotStringMessage in str(exception_info)51def test_raises_with_msg_if_second_not_string():52 with pytest.raises(ValueError) as exception_info:53 hamming.distance("TG", 3)...
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!!