Running Python unittest.main() verbosely

ben
Sat, 31 Jan 2009 03:03:12 +0000

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..