Unittest presents only total time spent on running all tests but does not present time spent on each test separately.

How to add timing of each test when using unittest?

share|improve this question
up vote 29 down vote accepted

I suppose, that it's not possible for now: http://bugs.python.org/issue4080.

But you can do something like this:

import unittest
import time

class SomeTest(unittest.TestCase):
    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print "%s: %.3f" % (self.id(), t)

    def testOne(self):
        time.sleep(1)
        self.assertEquals(int('42'), 42)

    def testTwo(self):
        time.sleep(2)
        self.assertEquals(str(42), '42')

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(SomeTest)
    unittest.TextTestRunner(verbosity=0).run(suite)

Result:

__main__.SomeTest.testOne: 1.001
__main__.SomeTest.testTwo: 2.002
----------------------------------------------------------------------
Ran 2 tests in 3.003s

OK
share|improve this answer
    
I guess it would be good to have this defined in some base class so that one could easily mix it in. – Piotr Dobrogost Feb 29 '12 at 17:33
    
Definitelly. This is only example of one possibility. – horejsek Feb 29 '12 at 18:08
    
@szeitlin No, it's method: docs.python.org/3/library/unittest.html#unittest.TestCase.id – horejsek Sep 28 '15 at 10:37

Nose tests with the pinnochio extension has a stopwatch option which will give you this, if nose is an option for you.

It also has a ton of other useful features and plugins to make using unittest nicer.

share|improve this answer

Here is variation of script from horejsek's answer. It will monkey-patch django TestCase so that every TestCase will give its total running time.

You can place this sript in the root package's __init__.py, where your settings.py lives. After that you can run tests with ./mange.py test -s

from django import test
import time


@classmethod
def setUpClass(cls):
    cls.startTime = time.time()


@classmethod
def tearDownClass(cls):
    print "\n%s.%s: %.3f" % (cls.__module__, cls.__name__, time.time() - cls.startTime)


test.TestCase.setUpClass = setUpClass
test.TestCase.tearDownClass = tearDownClass
share|improve this answer

Solution with command-line only:

1/ install nose (popular alternative test-runner) and extension pinnochio

$ pip install nose pinnochio

2/ run tests with time recording (times are saved in file .nose-stopwatch-times)

$ nosetests --with-stopwatch

3/ display tests names sorted by decreasing time:

$ python -c "import pickle,operator,signal; signal.signal(signal.SIGPIPE, signal.SIG_DFL); print '\n'.join(['%.03fms: %s'%(v[1],v[0]) for v in sorted(pickle.load(open('.nose-stopwatch-times','r')).items(), key=operator.itemgetter(1), reverse=True)])" | less
share|improve this answer
    
Nose is passe for a few years now. Everybody uses py.test and rightly so. – Piotr Dobrogost Nov 7 '17 at 14:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.