我在不同的文件夹里有十几个测试用例。在根目录中有一个测试运行程序。
unittest\
package1\
test1.py
test2.py
package2\
test3.py
test4.py
testrunner.py
目前,我将这四个测试用例手动添加到一个测试套件中
import unittest
from package1.test1 import Test1
from package1.test2 import Test2
from package2.test3 import Test3
from package2.test4 import Test4
suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(Test1))
suite.addTests(unittest.makeSuite(Test2))
suite.addTests(unittest.makeSuite(Test3))
suite.addTests(unittest.makeSuite(Test4))
result = unittest.TextTestRunner(verbosity=2).run(suite)
if not result.wasSuccessful():
sys.exit(1)
如何让测试运行程序自动测试所有测试用例?例如:
for testCase in findTestCases():
suite.addTests(testCase)
我做的是运行单独测试文件的包装脚本:
主包装纸^{} :
然后是另一个测试包装:
然后,在不同文件中的每个测试看起来像:
然后您可以轻松地从shell运行测试,如:
可以使用通配符指定特定目录中的所有文件,或者使用a new globbing option(
**
)递归运行所有测试(通过shopt -s globstar
启用)。在我看来,您应该切换到unittest2或具有发现特性的其他测试框架。发现测试是运行它们的一种非常明智的方法。
最为人所知的是:
例如,使用nosetest就足以从项目根目录运行
nosetests
,它将发现并运行找到的所有单元测试。很简单。还请注意,unittest2将包含在python 2.7中(我猜它会一直移植到2.4)。
上面的模块很好,但是当尝试输入参数时,nosetest可能会很有趣,而且速度也更快,可以很好地适应另一个模块。
如果要将结果添加到日志文件中,请改为在末尾添加:
同样,在模块底部,您正在测试如下位置代码:
相关问题 更多 >
编程相关推荐