From c1ea512b3aa5c3c4e730dd31a0e855cd111226f4 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Thu, 6 May 2021 14:51:27 +0530 Subject: [PATCH] feat: Indicate slow tests --- frappe/test_runner.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/frappe/test_runner.py b/frappe/test_runner.py index 08a5e579c2..6b890b8853 100644 --- a/frappe/test_runner.py +++ b/frappe/test_runner.py @@ -244,12 +244,6 @@ def _add_test(app, path, filename, verbose, test_suite=None, ui_tests=False): module_name = '{app}.{relative_path}.{module_name}'.format(app=app, relative_path=relative_path.replace('/', '.'), module_name=filename[:-3]) - test_module = importlib.import_module(f'{app}.tests') - - if hasattr(test_module, "global_test_dependencies"): - for doctype in test_module.global_test_dependencies: - make_test_records(doctype, verbose=verbose) - module = importlib.import_module(module_name) if hasattr(module, "test_dependencies"): @@ -439,6 +433,7 @@ def get_test_record_log(): class ParallelTestResult(unittest.TextTestResult): def startTest(self, test): + self._started_at = time.time() super(unittest.TextTestResult, self).startTest(test) test_class = unittest.util.strclass(test.__class__) if not hasattr(self, 'current_test_class') or self.current_test_class != test_class: @@ -450,7 +445,10 @@ class ParallelTestResult(unittest.TextTestResult): def addSuccess(self, test): super(unittest.TextTestResult, self).addSuccess(test) - click.echo(f" {click.style(' ✔ ', fg='green')} {self.getTestMethodName(test)}") + elapsed = time.time() - self._started_at + threshold_passed = elapsed >= SLOW_TEST_THRESHOLD + elapsed = click.style(f' ({elapsed:.03}s)', fg='red') if threshold_passed else '' + click.echo(f" {click.style(' ✔ ', fg='green')} {self.getTestMethodName(test)}{elapsed}") def addError(self, test, err): super(unittest.TextTestResult, self).addError(test, err) @@ -537,6 +535,8 @@ class ParallelTestRunner(): self.test_result = ParallelTestResult(stream=sys.stderr, descriptions=True, verbosity=2) self.test_status = 'ongoing' + self.make_test_records() + self.setup_coverage() while self.test_status == 'ongoing': self.run_tests_for_file(self.get_next_test()) @@ -561,6 +561,13 @@ class ParallelTestRunner(): self.test_status = response_data.get('status') return response_data.get('next_test') + def make_test_records(self): + test_module = importlib.import_module(f'{self.app}.tests') + + if hasattr(test_module, "global_test_dependencies"): + for doctype in test_module.global_test_dependencies: + make_test_records(doctype) + def run_tests_for_file(self, file_path): if not file_path: return