Annotate zerver.lib.test_runner.

This commit is contained in:
Tim Abbott
2016-06-04 23:05:39 -07:00
parent 1ca7c3378b
commit 15b2dd085e

View File

@@ -1,7 +1,8 @@
from __future__ import print_function from __future__ import print_function
from typing import Any, Set from typing import Any, Callable, Iterable, List, Optional, Set
from django.test import TestCase
from django.test.runner import DiscoverRunner from django.test.runner import DiscoverRunner
from django.test.signals import template_rendered from django.test.signals import template_rendered
@@ -17,6 +18,7 @@ import traceback
import unittest import unittest
def slow(expected_run_time, slowness_reason): def slow(expected_run_time, slowness_reason):
# type: (float, str) -> Callable[[Callable], Callable]
''' '''
This is a decorate that annotates a test as being "known This is a decorate that annotates a test as being "known
to be slow." The decorator will set expected_run_time and slowness_reason to be slow." The decorator will set expected_run_time and slowness_reason
@@ -24,6 +26,7 @@ def slow(expected_run_time, slowness_reason):
as needed, e.g. to exclude these tests in "fast" mode. as needed, e.g. to exclude these tests in "fast" mode.
''' '''
def decorator(f): def decorator(f):
# type: (Any) -> Any
f.expected_run_time = expected_run_time f.expected_run_time = expected_run_time
f.slowness_reason = slowness_reason f.slowness_reason = slowness_reason
return f return f
@@ -31,18 +34,22 @@ def slow(expected_run_time, slowness_reason):
return decorator return decorator
def is_known_slow_test(test_method): def is_known_slow_test(test_method):
# type: (Any) -> bool
return hasattr(test_method, 'slowness_reason') return hasattr(test_method, 'slowness_reason')
def full_test_name(test): def full_test_name(test):
# type: (TestCase) -> str
test_module = test.__module__ test_module = test.__module__
test_class = test.__class__.__name__ test_class = test.__class__.__name__
test_method = test._testMethodName test_method = test._testMethodName
return '%s.%s.%s' % (test_module, test_class, test_method) return '%s.%s.%s' % (test_module, test_class, test_method)
def get_test_method(test): def get_test_method(test):
# type: (TestCase) -> Callable[[], None]
return getattr(test, test._testMethodName) return getattr(test, test._testMethodName)
def enforce_timely_test_completion(test_method, test_name, delay): def enforce_timely_test_completion(test_method, test_name, delay):
# type: (Any, str, float) -> None
if hasattr(test_method, 'expected_run_time'): if hasattr(test_method, 'expected_run_time'):
# Allow for tests to run 50% slower than normal due # Allow for tests to run 50% slower than normal due
# to random variations. # to random variations.
@@ -57,9 +64,11 @@ def enforce_timely_test_completion(test_method, test_name, delay):
print('Test is TOO slow: %s (%.3f s)' % (test_name, delay)) print('Test is TOO slow: %s (%.3f s)' % (test_name, delay))
def fast_tests_only(): def fast_tests_only():
# type: () -> bool
return "FAST_TESTS_ONLY" in os.environ return "FAST_TESTS_ONLY" in os.environ
def run_test(test): def run_test(test):
# type: (TestCase) -> bool
failed = False failed = False
test_method = get_test_method(test) test_method = get_test_method(test)
@@ -128,6 +137,7 @@ class Runner(DiscoverRunner):
template_rendered.connect(self.on_template_rendered) template_rendered.connect(self.on_template_rendered)
def on_template_rendered(self, sender, context, **kwargs): def on_template_rendered(self, sender, context, **kwargs):
# type: (Any, Dict[str, Any], **Any) -> None
if hasattr(sender, 'template'): if hasattr(sender, 'template'):
template_name = sender.template.name template_name = sender.template.name
if template_name not in self.templates_rendered: if template_name not in self.templates_rendered:
@@ -138,9 +148,11 @@ class Runner(DiscoverRunner):
self.shallow_tested_templates.discard(template_name) self.shallow_tested_templates.discard(template_name)
def get_shallow_tested_templates(self): def get_shallow_tested_templates(self):
# type: () -> Set[str]
return self.shallow_tested_templates return self.shallow_tested_templates
def run_suite(self, suite, fatal_errors=None): def run_suite(self, suite, fatal_errors=True):
# type: (Iterable[TestCase], bool) -> bool
failed = False failed = False
for test in suite: for test in suite:
if run_test(test): if run_test(test):
@@ -150,6 +162,7 @@ class Runner(DiscoverRunner):
return failed return failed
def run_tests(self, test_labels, extra_tests=None, **kwargs): def run_tests(self, test_labels, extra_tests=None, **kwargs):
# type: (List[str], Optional[List[TestCase]], **Any) -> bool
self.setup_test_environment() self.setup_test_environment()
try: try:
suite = self.build_suite(test_labels, extra_tests) suite = self.build_suite(test_labels, extra_tests)
@@ -170,4 +183,3 @@ class Runner(DiscoverRunner):
failed = self.run_suite(suite, fatal_errors=kwargs.get('fatal_errors')) failed = self.run_suite(suite, fatal_errors=kwargs.get('fatal_errors'))
self.teardown_test_environment() self.teardown_test_environment()
return failed return failed
print()