Unittest Framework 简明教程
UnitTest Framework - Time Test
Java 单元测试框架 Junit(Pyunit 是 JUnit 的实现)有一个便捷的超时选项。如果测试花费的时间超过指定的时间,则该测试将标记为失败。
Junit, the Java unit testing framework (Pyunit is implementation of JUnit) has a handy option of timeout. If a test takes more than specified time, it will be marked as failed.
Python 的测试框架不包含任何超时支持。但是,一个名为 timeout-decorator 的第三方模块可以完成该作业。
Python’s testing framework doesn’t contain any support for time out. However, a third part module called timeout-decorator can do the job.
从以下位置下载并安装该模块 −
Download and install the module from −
-
Import timeout_decorator in the code
-
Put timeout decorator before the test
-
@timeout_decorator.timeout(10)
如果这个时间段中的测试方法超过这里提到的超时(10 分钟),将会引发 TimeOutError。例如 −
If a test method below this line takes more than the timeout mentioned (10 mins) here, a TimeOutError will be raised. For example −
import time
import timeout_decorator
class timeoutTest(unittest.TestCase):
@timeout_decorator.timeout(5)
def testtimeout(self):
print "Start"
for i in range(1,10):
time.sleep(1)
print "%d seconds have passed" % i
if __name__ == '__main__':
unittest.main()