我正在从 unittest 转换为 testtools 以并行运行我的 Python 测试。
我想知道在使用 testtools.ConcurrentStreamTestSuite 时是否有办法限制同时运行的测试数量?我很快就会有数百个测试,并且需要将执行限制为一次最多 10 个。其余的将排队等待线程释放时执行。
如果是这样,您能否分享一些展示如何做到这一点的代码片段?
请您参考如下方法:
我已经清理了逻辑,这是该函数的新版本(效果更好):
def split_suite_into_chunks(num_threads, suite):
# Compute num_threads such that the number of threads does not exceed the value passed to the function
# Keep num_threads to a reasonable number of threads
if num_threads < 0: num_threads = 1
if num_threads > 8: num_threads = 8
num_tests = suite.countTestCases()
s = []
s_tmp = unittest.TestSuite()
n = round(num_tests / num_threads)
for case in suite:
if n <= 0 and s_tmp.countTestCases() > 0:
s.append([s_tmp, None])
num_threads -= 1
num_tests -= s_tmp.countTestCases()
s_tmp = unittest.TestSuite()
n = round(num_tests / num_threads)
s_tmp.addTest(case)
n -= 1
if s_tmp.countTestCases() > 0:
if s_tmp.countTestCases() > 0: s.append([s_tmp, None])
num_tests -= s_tmp.countTestCases()
if num_tests != 0: print("Error: num_tests should be 0 but is %s!" % num_tests)
return s
