Best Python code snippet using autotest_python
tests.py
Source:tests.py  
1from custom_test_helpers import run_test_case_tests, inspect_assertions, \2    normalize_call_args, check_used_only_assertions3from test_helper import passed, failed, run_common_tests, import_task_file, \4    test_answer_placeholders_text_deleted5import itertools6import unittest.mock7from tested_code import find_foo8import re9def check_test_not_42():10    has_failed = False11    task_module = import_task_file()12    test_case_class = inspect_assertions(task_module.TestRandomNot42)13    test_result = run_test_case_tests(test_case_class)14    if not test_result.wasSuccessful():15        failed("at least one test failed")16        has_failed = True17    if not check_used_only_assertions(test_case_class, ["assertNotEqual"]):18        has_failed = True19    mock_random_not_42 = unittest.mock.Mock(return_value=42)20    with unittest.mock.patch('tested_code.random_not_42', mock_random_not_42):21        task_module = import_task_file()22        test_result = run_test_case_tests(task_module.TestRandomNot42)23        if test_result.wasSuccessful():24            failed("tests passed with broken implementation")25            has_failed = True26    if not has_failed:27        passed()28def check_test_find_foo():29    has_failed = False30    task_module = import_task_file()31    test_case_class = inspect_assertions(task_module.TestFindFoo)32    test_result = run_test_case_tests(test_case_class)33    if not test_result.wasSuccessful():34        failed("at least one test failed")35        has_failed = True36    if not check_used_only_assertions(37            test_case_class,38            ["assertIsNone", "assertIsNotNone"],39    ):40        has_failed = True41    # check that all of the substrings were tested42    mock_find_foo = unittest.mock.Mock(wraps=find_foo)43    with unittest.mock.patch('tested_code.find_foo', mock_find_foo):44        task_module = import_task_file()45        test_case_class = task_module.TestFindFoo46        run_test_case_tests(test_case_class)47        normalized_call_args = [48            normalize_call_args(call_args, func=find_foo)49            for call_args in mock_find_foo.call_args_list50        ]51        for substring in itertools.chain(52                ["foo"],53                test_case_class.strings_with_foo,54                test_case_class.strings_without_foo55        ):56            if ((substring,), {}) not in normalized_call_args:57                failed("substring \"{}\" not tested".format(substring))58                has_failed = True59    # check with broken find_foo()60    def find_fo(s):61        return re.search(r"fo", s)62    for broken_find_fo in [63        find_fo,64        lambda s: None,65        lambda s: 0,66    ]:67        with unittest.mock.patch('tested_code.find_foo', broken_find_fo):68            task_module = import_task_file()69            test_result = run_test_case_tests(task_module.TestFindFoo)70            if test_result.wasSuccessful():71                failed("tests passed with broken implementation")72                has_failed = True73    if not has_failed:74        passed()75def check_test_random_float_between_inclusive():76    has_failed = False77    task_module = import_task_file()78    test_case_class = inspect_assertions(task_module.TestRandomFloatBetweenInclusive)79    test_result = run_test_case_tests(test_case_class)80    if not test_result.wasSuccessful():81        failed("at least one test failed")82        has_failed = True83    if not check_used_only_assertions(84            test_case_class,85            ["assertGreaterEqual", "assertLessEqual"],86    ):87        has_failed = True88    for broken_func in [89        lambda a, b: a - 1,90        lambda a, b: b + 1,91    ]:92        with unittest.mock.patch('tested_code.random_float_between_inclusive', broken_func):93            task_module = import_task_file()94            test_result = run_test_case_tests(task_module.TestRandomFloatBetweenInclusive)95            if test_result.wasSuccessful():96                failed("tests passed with broken implementation")97                has_failed = True98    if not has_failed:99        passed()100def check_test_random_float_between_noninclusive():101    has_failed = False102    task_module = import_task_file()103    test_case_class = inspect_assertions(task_module.TestRandomFloatBetweenNoninclusive)104    test_result = run_test_case_tests(test_case_class)105    if not test_result.wasSuccessful():106        failed("at least one test failed")107        has_failed = True108    if not check_used_only_assertions(109            test_case_class,110            ["assertGreater", "assertLess"],111    ):112        has_failed = True113    for broken_func in [114        lambda a, b: a - 1,115        lambda a, b: b + 1,116        lambda a, b: a,117        lambda a, b: b,118    ]:119        with unittest.mock.patch('tested_code.random_float_between_noninclusive', broken_func):120            task_module = import_task_file()121            test_result = run_test_case_tests(task_module.TestRandomFloatBetweenNoninclusive)122            if test_result.wasSuccessful():123                failed("tests passed with broken implementation")124                has_failed = True125    if not has_failed:126        passed()127if __name__ == '__main__':128    run_common_tests()129    test_answer_placeholders_text_deleted()130    check_test_not_42()131    check_test_find_foo()132    check_test_random_float_between_inclusive()...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!!
