xli*_*iiv 7 python test-suite python-unittest
如何列出所有发现的测试?我找到了这个命令:
python3.4 -m unittest discover -s .
Run Code Online (Sandbox Code Playgroud)
但这并不是我想要的,因为上面的命令执行测试.我的意思是让我们有一个包含大量测试的项目.执行时间是几分钟.这迫使我等到测试结束.
我想要的是这样的事情(上面的命令输出)
test_choice (test.TestSequenceFunctions) ... ok
test_sample (test.TestSequenceFunctions) ... ok
test_shuffle (test.TestSequenceFunctions) ... ok
Run Code Online (Sandbox Code Playgroud)
甚至更好,更像这样的东西(在上面编辑之后):
test.TestSequenceFunctions.test_choice
test.TestSequenceFunctions.test_sample
test.TestSequenceFunctions.test_shuffle
Run Code Online (Sandbox Code Playgroud)
但是没有执行,只打印测试用于复制和粘贴目的的"路径".
vau*_*tah 15
命令行命令discover使用unittest.TestLoader.这是一个有点优雅的解决方案
import unittest
def print_suite(suite):
if hasattr(suite, '__iter__'):
for x in suite:
print_suite(x)
else:
print(suite)
print_suite(unittest.defaultTestLoader.discover('.'))
Run Code Online (Sandbox Code Playgroud)
运行示例:
In [5]: print_suite(unittest.defaultTestLoader.discover('.'))
test_accounts (tests.TestAccounts)
test_counters (tests.TestAccounts)
# More of this ...
test_full (tests.TestImages)
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为TestLoader.discover 返回TestSuite实现__iter__方法的对象,因此是可迭代的.