Python unittest - 使用自定义 TestSuite 调用 unittest.main() [英] Python unittest - invoke unittest.main() with a custom TestSuite
问题描述
我正在使用 python 进行单元测试.我没有使用任何自动测试发现.我正在手动将 TestCases
组装成 TestSuite
.
I'm making unittests with python. I am not using any automatical test discovery. I am assembling TestCases
into a TestSuite
manually.
我可以使用 unittest.TextTestRunner().run(suite)
运行这些测试,我想使用 unittest.main()
运行它们,以便我可以使用命令行选项(如 -v
/--failfast
).文档说 unittest.main()
可以采用 TestRunner 选项.
I can run these tests with unittest.TextTestRunner().run(suite)
, I would like to run them with unittest.main()
so that I can use command line options (like -v
/--failfast
). The documentation says that unittest.main()
can take a TestRunner option.
如何将我的 TestSuite
转换为 TestRunner
?
How to convert my TestSuite
into a TestRunner
?
推荐答案
与 如何使用 unittest.main 运行测试套件(回答复制粘贴):
Near duplicate of How to run a testsuite with unittest.main (answer copy-and-pasted):
您不能将 TestSuite
传递给 main,请查看 unittest.main.TestProgram
的构造函数(即 unittest.main
> 实际上是)以及这个类是如何工作的.如果有的话,第一个参数是模块名称,而不是测试套件.
You can't pass a TestSuite
to main, check out the constructor of unittest.main.TestProgram
(which is was unittest.main
actually is) and how this class works. The first argument if anything is the module name, not a testsuite.
main()
实际上从 sys.argv
获取它的参数,因为它实际上打算从命令行使用而不是从程序中使用.这样做只是为了方便.
main()
actually takes its arguments from sys.argv
, as it is actually intended to be used from the command line and not from within a program. It's just common to do so for convenience.
这篇关于Python unittest - 使用自定义 TestSuite 调用 unittest.main()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!