Python unittest - invoke unittest.main() with a custom TestSuite
I'm making unittests with python. I am not using any automatical test disc开发者_运维百科overy. I am assembling TestCases
into a TestSuite
manually.
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.
How to convert my TestSuite
into a TestRunner
?
Near duplicate of How to run a testsuite with unittest.main (answer copy-and-pasted):
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()
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.
Do nothing except be sure you have this in your unit test module.
if __name__ == '__main__':
unittest.main(failfast=True)
http://docs.python.org/library/unittest.html#unittest.main
From the documentation...
unittest.main( failfast=True, testRunner=unittest.TextTestRunner )
精彩评论