[英]Get python unit test duration in seconds
Is there any way to get the total amount of time that "unittest.TextTestRunner().run()" has taken to run a specific unit test.有没有办法获得“unittest.TextTestRunner().run()”运行特定单元测试所花费的总时间。
I'm using a for loop to test modules against certain scenarios (some having to be used and some not, so they run a few times), and I would like to print the total time it has taken to run all the tests.我正在使用 for 循环来针对某些场景测试模块(有些必须使用,有些不需要,所以它们运行了几次),我想打印运行所有测试所花费的总时间。
Any help would be greatly appreciated.任何帮助将不胜感激。
UPDATED , thanks to @Centralniak's comment.更新,感谢@Centralniak 的评论。
How about simple简单的怎么样
from datetime import datetime
tick = datetime.now()
# run the tests here
tock = datetime.now()
diff = tock - tick # the result is a datetime.timedelta object
print(diff.total_seconds())
您可以在设置功能中记录开始时间,然后在清理中打印经过的时间。
I do this exactly as Eric postulated -- here's a decorator I use for tests (often more functional-test-y than strict unit tests)...我完全按照 Eric 的假设来做这件事——这是我用于测试的装饰器(通常比严格的单元测试更多的功能测试)......
# -*- coding: utf-8 -*-
from __future__ import print_function
from functools import wraps
from pprint import pprint
WIDTH = 60
print_separator = lambda fill='-', width=WIDTH: print(fill * width)
def timedtest(function):
"""
Functions so decorated will print the time they took to execute.
Usage:
import unittest
class MyTests(unittest.TestCase):
@timedtest
def test_something(self):
assert something is something_else
# … etc
# An optional return value is pretty-printed,
# along with the timing values:
return another_thing
"""
@wraps(function)
def wrapper(*args, **kwargs):
print()
print("TESTING: %s(…)" % getattr(function, "__name__", "<unnamed>"))
print_separator()
print()
t1 = time.time()
out = function(*args, **kwargs)
t2 = time.time()
dt = str((t2 - t1) * 1.00)
dtout = dt[:(dt.find(".") + 4)]
print_separator()
if out is not None:
print('RESULTS:')
pprint(out, indent=4)
print('Test finished in %s seconds' % dtout)
print_separator('=')
return out
return wrapper
That's the core of it -- from there, if you want, you can stash the times in a database for analysis, or draw graphs, et cetera.这就是它的核心——从那里,如果您愿意,您可以将时间存储在数据库中以进行分析,或绘制图形等。 A decorator like this (using @wraps(…)
from the functools
module) won't interfere with any of the dark magic that unit-test frameworks occasionally resort to.像这样的装饰器(使用functools
模块中的@wraps(…)
)不会干扰单元测试框架偶尔使用的任何黑暗魔法。
Following Eric's one-line answer I have a little snippet I work with here:按照埃里克的单行回答,我在这里使用了一个小片段:
from datetime import datetime
class SomeTests(unittest.TestCase):
"""
... write the rest yourself! ...
"""
def setUp(self):
self.tick = datetime.now()
def tearDown(self):
self.tock = datetime.now()
diff = self.tock - self.tick
print (diff.microseconds / 1000), "ms"
# all the other tests below
This works fine enough for me, for now, but I want to fix some minor formatting issues.这对我来说已经足够好了,现在,但我想修复一些小的格式问题。 The result ok
is now on the next line, and FAIL
has priority.结果ok
现在在下一行,并且FAIL
具有优先权。 This is ugly.这是丑陋的。
Besides using datetime
, you could also use time
除了使用datetime
,您还可以使用time
from time import time
t0 = time()
# do your stuff here
print(time() - t0) # it will show in seconds
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.