如何使用 unittest.main 运行测试套件 [英] How to run a testsuite with unittest.main
问题描述
可能的重复:
Python unittest - 使用自定义 TestSuite 调用 unittest.main()
我有一个用例如创建的测试套件
I have a testsuite created with e.g.
suite = unittest.TestSuite()
suite.addTest(module1.MyTest("test_simple"))
suite.addTest(module2.MyTest("test_simple"))
并且需要使用 unitest.main()
开始这些测试.当仅尝试 unitest.main(suite)
时,根本不会运行任何测试.如何在不调用 test.TextTestRunner
或类似代码的情况下使用 unittest.main()
执行此操作?
and need to start these tests with unitest.main()
. When trying just unitest.main(suite)
no tests are run at all. How can I do this with unittest.main()
without calling test.TextTestRunner
or similar?
推荐答案
您不能将 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.
这篇关于如何使用 unittest.main 运行测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!