Pio*_*ost 34 python performance unit-testing
Unittest仅显示运行所有测试所花费的总时间,但不会分别显示每个测试所花费的时间.
使用unittest时如何添加每个测试的时间?
hor*_*sek 36
我想,现在不可能: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)
Run Code Online (Sandbox Code Playgroud)
结果:
__main__.SomeTest.testOne: 1.001
__main__.SomeTest.testTwo: 2.002
----------------------------------------------------------------------
Ran 2 tests in 3.003s
OK
Run Code Online (Sandbox Code Playgroud)
使用pinnochio扩展的鼻子测试有一个秒表选项,如果鼻子是你的选择,它会给你这个.
它还有许多其他有用的功能和插件,可以使用unittest更好.
这是horejsek答案的脚本变体.它将修补django TestCase,以便每个TestCase都能给出它的总运行时间.
您可以将此sript放在您的settings.py所在的根包__init__.py中.之后,您可以使用./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
Run Code Online (Sandbox Code Playgroud)
PyCharm CE(免费)提供了清晰的持续时间视图unittest。它还有助于按目录结构和文件进行聚合,并允许您按持续时间排序:
正如 @horejsek 提到的,unittest使用开放 PR 添加持续时间测量存在一个问题: https: //github.com/python/cpython/pull/12271。
仅使用命令行解决方案:
1 /安装nose(流行的替代测试运行程序)和扩展pinnochio
$ pip install nose pinnochio
Run Code Online (Sandbox Code Playgroud)
2 /运行带有时间记录的测试(时间保存在文件中.nose-stopwatch-times)
$ nosetests --with-stopwatch
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
您可以使用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)
Run Code Online (Sandbox Code Playgroud)
如果您查看django_slowtests/test_runner.py您也可以自己调整该技术。
| 归档时间: |
|
| 查看次数: |
10879 次 |
| 最近记录: |