test_runner.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from time import time
  2. from unittest.runner import TextTestResult, TextTestRunner
  3. from django.test.runner import DiscoverRunner
  4. class TimedTextTestResult(TextTestResult):
  5. def __init__(self, *args, **kwargs):
  6. super(TimedTextTestResult, self).__init__(*args, **kwargs)
  7. self.clocks = dict()
  8. def startTest(self, test):
  9. self.clocks[test] = time()
  10. super(TextTestResult, self).startTest(test)
  11. if self.showAll:
  12. self.stream.write(self.getDescription(test))
  13. self.stream.write(" ... ")
  14. self.stream.flush()
  15. def addSuccess(self, test):
  16. super(TextTestResult, self).addSuccess(test)
  17. if self.showAll:
  18. self.stream.writeln("runtime (%.6fs)" % (time() - self.clocks[test]))
  19. elif self.dots:
  20. self.stream.write(".")
  21. self.stream.flush()
  22. class TimedTextTestRunner(TextTestRunner):
  23. resultclass = TimedTextTestResult
  24. class TimedTestRunner(DiscoverRunner):
  25. """To view timings, use ./manage.py test -v 2"""
  26. test_runner = TimedTextTestRunner