2012-02-29 90 views

回答

30

我想,這是不可能的現在:http://bugs.python.org/issue4080

但你可以做這樣的事情:

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) 

結果:

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

OK 
+0

我想這將是很好的有一些基類中定義這使人們可以很容易地在。 – 2012-02-29 17:33:32

+0

Definitelly它混合。這只是一種可能性的例子。 – horejsek 2012-02-29 18:08:54

+0

@szeitlin不,它的方法:https://docs.python.org/3/library/unittest.html#unittest.TestCase.id – horejsek 2015-09-28 10:37:36

5

這是從horejsek的答案腳本的變化。 它將monkey-patch django TestCase這樣每個TestCase會給它的總運行時間。

你可以把這個sript根包的__init__.py,你的settings.py生活在那裏。 之後,你可以與./mange.py測試運行測試-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 
2

解僅命令行:

1 /安裝nose(流行的替代測試澆道)和延伸pinnochio

$ pip install nose pinnochio 

2 /隨時間記錄(次保存在文件.nose-stopwatch-times

運行測試
$ nosetests --with-stopwatch 
通過減少時間排序

3 /顯示測試名稱:

$ python -c "import pickle,operator,signal; signal.signal(signal.SIGPIPE, signal.SIG_DFL); print '\n'.join(['%.03fs: %s'%(v[1],v[0]) for v in sorted(pickle.load(open('.nose-stopwatch-times','r')).items(), key=operator.itemgetter(1), reverse=True)])" | less 
+0

鼻子已經流行了好幾年了。每個人都使用py.test,正確如此。 – 2017-11-07 14:46:46

+0

我認爲當它打印's'時會打印'ms' – 2018-02-20 21:26:52

0

您可以使用django-slowtests,它提供了這樣的輸出:

$ python manage.py test 
Creating test database for alias 'default'... 
.......... 
---------------------------------------------------------------------- 
Ran 10 tests in 0.413s 

OK 
Destroying test database for alias 'default'... 

Ten slowest tests: 
0.3597s test_detail_view_with_a_future_poll (polls.tests.PollIndexDetailTests) 
0.0284s test_detail_view_with_a_past_poll (polls.tests.PollIndexDetailTests) 
0.0068s test_index_view_with_a_future_poll (polls.tests.PollViewTests) 
0.0047s test_index_view_with_a_past_poll (polls.tests.PollViewTests) 
0.0045s test_index_view_with_two_past_polls (polls.tests.PollViewTests) 
0.0041s test_index_view_with_future_poll_and_past_poll (polls.tests.PollViewTests) 
0.0036s test_index_view_with_no_polls (polls.tests.PollViewTests) 
0.0003s test_was_published_recently_with_future_poll (polls.tests.PollMethodTests) 
0.0002s test_was_published_recently_with_recent_poll (polls.tests.PollMethodTests) 
0.0002s test_was_published_recently_with_old_poll (polls.tests.PollMethodTests) 

如果你看看django_slowtests/test_runner.py你也可以適應技巧你自己。

相關問題