Running Python unittest.main() verbosely
Posted on Jan. 31, 2009 by Ben Dickson.
This is an old post from 2009. The content may be outdated or no longer relevant.
This is an old post I wrote, which was originally posted on neverfear.org on Sat, 31st Jan 2009 13:33:12. Archived here for posterity.
I want to run the unittests verbosely, so you can see which test is running (the function name, or if you have a one-line docstring, it displays that), instead of the usual "..E..F." output.
There are two ways, the first is to construct a test-suite using
TestLoader
, pass it to
TestTextLoader(verbosity=2)
:
suite = unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(test_tvnamer.test_name_parser),
unittest.TestLoader().loadTestsFromTestCase(test_tvdb_api.test_tvdb)
])
# for one test, you can do:
# suite = unittest.TestLoader().loadTestsFromTestCase(test_name_parser)
runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)
test_name_parser and test_tvdb are the classes containing my tests.
The other simpler (but less flexible) method is to simply use the unittest.main() function, but with a verbose test runner:
runner = unittest.TextTestRunner(verbosity = 2)
unittest.main(testRunner = runner)
This will run like a regular
unittest.main()
call (doing automagic test discovery), but verbosely.
I'm posting this because the only solution I found in a quick search was to append
--verbose
to
sys.argv
so the unittest runner thought it was specified on the command line, which is horrible..