如何在多核机器上加速Python的'unittest'?
4 个回答
0
你可以考虑使用 multiprocessing
这个库,让每个测试在不同的进程中运行。这意味着每个单元测试(或者一组单元测试)应该是独立的,不需要共享状态。这样做会开启其他进程,并且会利用其他的处理器核心。
具体可以查看这个页面上的“使用工作池”( http://docs.python.org/library/multiprocessing.html#using-a-pool-of-workers)
编辑: 这个模块从2.6版本开始就包含在内了。
4
testtools这个包是对unittest的一个扩展,它支持同时运行多个测试。你可以把它和你以前写的那些继承自unittest.TestCase
的测试类一起使用。
举个例子:
import unittest
import testtools
class MyTester(unittest.TestCase):
# Tests...
suite = unittest.TestLoader().loadTestsFromTestCase(MyTester)
concurrent_suite = testtools.ConcurrentStreamTestSuite(lambda: ((case, None) for case in suite))
concurrent_suite.run(testtools.StreamResult())