def enforce_timely_test_completion(test_method: Any, test_name: str, delay: float, result: TestResult) -> None: if hasattr(test_method, 'slowness_reason'): max_delay = 2.0 # seconds else: max_delay = 0.4 # seconds if delay > max_delay: msg = '** Test is TOO slow: %s (%.3f s)\n' % (test_name, delay) result.addInfo(test_method, msg)
def run_test(test: TestCase, result: TestResult) -> bool: failed = False test_method = get_test_method(test) if fast_tests_only() and is_known_slow_test(test_method): return failed test_name = full_test_name(test) bounce_key_prefix_for_testing(test_name) bounce_redis_key_prefix_for_testing(test_name) flush_caches_for_testing() if not hasattr(test, "_pre_setup"): # test_name is likely of the form unittest.loader.ModuleImportFailure.zerver.tests.test_upload import_failure_prefix = 'unittest.loader.ModuleImportFailure.' if test_name.startswith(import_failure_prefix): actual_test_name = test_name[len(import_failure_prefix):] error_msg = ("\nActual test to be run is %s, but import failed.\n" "Importing test module directly to generate clearer " "traceback:\n") % (actual_test_name, ) result.addInfo(test, error_msg) try: command = [ sys.executable, "-c", "import %s" % (actual_test_name, ) ] msg = "Import test command: `%s`" % (' '.join(command), ) result.addInfo(test, msg) subprocess.check_call(command) except subprocess.CalledProcessError: msg = ("If that traceback is confusing, try doing the " "import inside `./manage.py shell`") result.addInfo(test, msg) result.addError(test, sys.exc_info()) return True msg = ("Import unexpectedly succeeded! Something is wrong. Try " "running `import %s` inside `./manage.py shell`.\n" "If that works, you may have introduced an import " "cycle.") % (actual_test_name, ) import_error = (Exception, Exception(msg), None ) # type: Tuple[Any, Any, Any] result.addError(test, import_error) return True else: msg = "Test doesn't have _pre_setup; something is wrong." error_pre_setup = (Exception, Exception(msg), None ) # type: Tuple[Any, Any, Any] result.addError(test, error_pre_setup) return True test._pre_setup() start_time = time.time() test(result) # unittest will handle skipping, error, failure and success. delay = time.time() - start_time enforce_timely_test_completion(test_method, test_name, delay, result) slowness_reason = getattr(test_method, 'slowness_reason', '') TEST_TIMINGS.append((delay, test_name, slowness_reason)) test._post_teardown() return failed