is_error_or_failure = test_method_name in test_method_name2failure_or_error if expected_result is True and is_error_or_failure: failed("{func_name} failed test {test_name}, which it should pass".format( func_name=broken_func_name, test_name="TestRandomShuffle."+test_method_name, ), name="test {}".format(broken_func_name)) has_failed = True message = [x[1] for x in test_result.failures + test_result.errors if x[0].id().rsplit('.', 1)[1] == test_method_name][0] print(message) elif expected_result is False and not is_error_or_failure: failed("{func_name} passed test {test_name}, which it should fail".format( func_name=broken_func_name, test_name="TestRandomShuffle."+test_method_name, ), name="test {}".format(broken_func_name)) has_failed = True if not has_failed: passed(name="test {}".format(broken_func_name)) task_module.random_shuffle = orig_random_shuffle if __name__ == '__main__': run_common_tests() test_answer_placeholders_text_deleted() check_tests_pass(import_task_file()) check_tests_fail_on_broken_implementations()
from custom_test_helpers import check_tests_pass, check_tests_fail, \ reload_module, abort_tests from test_helper import run_common_tests, test_answer_placeholders_text_deleted, \ passed, failed, import_task_file if __name__ == '__main__': from hello_someone import hello_someone run_common_tests() test_answer_placeholders_text_deleted() task_tests_module = import_task_file() # check that all tests pass check_tests_pass(task_tests_module) # check that the function hello_someone() is called at least once by the tests counting_hello_someone = mock.Mock(wraps=hello_someone) with mock.patch('{}.{}'.format('hello_someone', 'hello_someone'), counting_hello_someone): module = import_task_file() check_tests_pass(module) if counting_hello_someone.call_count > 0: passed("Test called the 'hello_someone' function") else: failed("Test never called the 'hello_someone' function") # check that the tests fail on a broken implementation def broken_hello_someone(someone): # omit the comma return "Hello {}!".format(someone)
from custom_test_helpers import check_tests_pass, check_tests_fail, \ reload_module, abort_tests from test_helper import run_common_tests, test_answer_placeholders_text_deleted, \ passed, failed, import_task_file if __name__ == '__main__': from hello_someone import hello_someone run_common_tests() test_answer_placeholders_text_deleted() task_tests_module = import_task_file() # check that all tests pass check_tests_pass(task_tests_module) # check that the function hello_someone() is called at least once by the tests counting_hello_someone = mock.Mock(wraps=hello_someone) with mock.patch('{}.{}'.format('hello_someone', 'hello_someone'), counting_hello_someone): module = import_task_file() check_tests_pass(module) if counting_hello_someone.call_count > 0: passed("Test called the 'hello_someone' function") else: failed("Test never called the 'hello_someone' function") # check that the tests fail on a broken implementation def broken_hello_someone(someone): # omit the comma
), name="test {}".format(broken_func_name)) has_failed = True message = [ x[1] for x in test_result.failures + test_result.errors if x[0].id().rsplit('.', 1)[1] == test_method_name ][0] print(message) elif expected_result is False and not is_error_or_failure: failed( "{func_name} passed test {test_name}, which it should fail" .format( func_name=broken_func_name, test_name="TestRandomShuffle." + test_method_name, ), name="test {}".format(broken_func_name)) has_failed = True if not has_failed: passed(name="test {}".format(broken_func_name)) task_module.random_shuffle = orig_random_shuffle if __name__ == '__main__': run_common_tests() test_answer_placeholders_text_deleted() check_tests_pass(import_task_file()) check_tests_fail_on_broken_implementations()
from custom_test_helpers import check_tests_pass from test_helper import run_common_tests, test_answer_placeholders_text_deleted, import_task_file if __name__ == '__main__': run_common_tests() test_answer_placeholders_text_deleted() module = import_task_file() check_tests_pass(module)