Tweak runner
# https://hakibenita.com/timing-tests-in-python-for-fun-and-profit
import time
-import unittest
from unittest import TextTestRunner
from unittest.runner import TextTestResult
from django.test.runner import DiscoverRunner
-SLOW_TEST_THRESHOLD = 0.2
+SLOW_TEST_THRESHOLD = 1.5
class TimeLoggingTestResult(TextTestResult):
@@ -20,11 +19,12 @@ class TimeLoggingTestResult(TextTestResult):
self._test_started_at = time.time()
super().startTest(test)
- def addSuccess(self, test):
- elapsed = time.time() - self._test_started_at
+ def stopTest(self, test):
+ self._test_ended_at = time.time()
+ elapsed = round(self._test_ended_at - self._test_started_at, 5)
name = self.getDescription(test)
self.test_timings.append((name, elapsed))
- super().addSuccess(test)
+ super().stopTest(test)
def getTestTimings(self):
return self.test_timings
@@ -39,10 +39,20 @@ class TimeLoggingTextTestRunner(TextTestRunner):
def run(self, test):
result = super().run(test)
- self.stream.writeln("\nSlow Tests (>{:.03}s):".format(self.slow_test_threshold))
- for name, elapsed in result.getTestTimings():
+ timings = result.getTestTimings()
+ timings_sorted = sorted(timings, key=lambda y: y[1], reverse=True)
+
+ self.stream.writeln("\nSlow tests (>{:.05}s):".format(self.slow_test_threshold))
+ slow_test_count = 0
+ for name, elapsed in timings_sorted:
if elapsed > self.slow_test_threshold:
- self.stream.writeln("({:.03}s) {}".format(elapsed, name))
+ slow_test_count += 1
+ self.stream.writeln("({:.05}s) {}".format(elapsed, name))
+ self.stream.writeln(
+ "\nFound {} slow tests (>{:.05}s) out of {}.\n".format(
+ slow_test_count, self.slow_test_threshold, len(timings_sorted)
+ )
+ )
return result
