Skip to content

Instantly share code, notes, and snippets.

@Gautier
Created May 17, 2012 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gautier/2717677 to your computer and use it in GitHub Desktop.
Save Gautier/2717677 to your computer and use it in GitHub Desktop.
Prints the 5 slowest tests and their running time in ms
"""
>>> import unittest
>>> class MyTestCase(TestsTimer, unittest.TestCase):
... def test_add(self):
... self.assertEqual(1 + 2, 3)
>>> suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
>>> unittest.TextTestRunner(verbosity=0).run(suite) # doctest:+ELLIPSIS
[('test_add', ...)]
<unittest.runner.TextTestResult run=1 errors=0 failures=0>
"""
from operator import itemgetter
from pprint import pprint
import time
class TestsTimer(object):
"Prints the 5 slowest tests case and their running time in ms"
results = []
t0 = 0
@classmethod
def tearDownClass(cls):
pprint(sorted(TestsTimer.results, key=itemgetter(1), reverse=True)[:5])
def setUp(self):
TestsTimer.t0 = time.time()
def tearDown(self):
t = int((time.time() - TestsTimer.t0) * 1000)
TestsTimer.results.append((self._testMethodName, t))
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment