How to know time spent on each test when using unittest?

前端 未结 7 1384
-上瘾入骨i
-上瘾入骨i 2021-02-05 00:37

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?

7条回答
  •  醉话见心
    2021-02-05 01:25

    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
    

提交回复
热议问题