如何在Python中自动将几十个测试用例添加到测试套件中
我在不同的文件夹中有几十个测试用例。在根目录中有一个测试运行程序。
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)
i have dozen of test cases in different folders. In the root directory there is a test runner.
unittest\
package1\
test1.py
test2.py
package2\
test3.py
test4.py
testrunner.py
Currently I added the four test cases manually into a test suite
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)
How to let test runner test all test cases automatically? Such as:
for testCase in findTestCases():
suite.addTests(testCase)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,您应该切换到 unittest2 或其他具有发现功能的测试框架。发现测试是一种非常明智的运行方式。
最知名的是:
例如,使用nosetest就足以从项目根目录运行
nosetests
,它将发现并运行所有它找到的单元测试。很简单。还要注意,unittest2 将包含在 python 2.7 中(我猜会向后移植到 2.4)。
In my opinion you should switch to unittest2 or other test frameworks with discovery features. Discovery tests is a really sane way to run them.
Most known are:
For example with nosetest is sufficient to run
nosetests
from the project root directory and it will discover and run all the unit tests it find. Pretty simple.Notice also that unittest2 will be included in python 2.7 (and backported till 2.4 I guess).
上面的模块很好,但是当尝试输入参数时,NoseTests 可能很有趣,而且这也更快并且很好地适合另一个模块。
如果您想将结果添加到日志文件中,请在末尾添加以下内容:
也在您正在测试的模块的底部放置如下代码:
The above modules are good but NoseTests can be funny when trying to enter in parameters and this is also faster and fit's into another module nicely.
If you want to add results to a log file add this at the end instead:
Also at the bottom of the modules you are testing place code like this:
我所做的是一个运行单独测试文件的包装器脚本:
主包装器 < code>run_tests.py:
然后是另一个测试包装器:
然后单独文件中的每个测试将如下所示:
然后您可以轻松地从 shell 运行测试,例如:
您可以使用通配符指定特定文件中的所有文件目录,或使用新的通配选项 (
**
)以递归方式运行所有测试(通过shopt -s globstar
启用)。What I did is a wrapper script which running separate test files:
Main wrapper
run_tests.py
:Then another wrapper for tests:
Then each test in separate files would look like:
Then you can easily run tests from shell like:
You can use wildcard to specify all files within certain directories, or use a new globbing option (
**
) to run all tests recursively (enable byshopt -s globstar
).