Welcome, guest | Sign In | My Account | Store | Cart

When running unit tests, using the verbose flag often provides an extra level of protection against mistakes. When running from the command line, this simply means adding the –v option. If you use an IDE, matters become more complicated. While you can often set your IDE to pass in the –v option when running a file, this has a number of drawbacks. This code will ensure that your tests will run with the verbose option.

Python, 6 lines
1
2
3
4
5
6
#omitted unit test code.

if __name__ == "__main__":
        import sys
        sys.argv.append('-v')
        unittest.main()

Since arguments are stored as a list, you can simply modify that list before calling unittest.main(). Note that there is no problem if you use the above code AND pass in the –v option. (Duplicate flags are not a problem in this case.)

2 comments

Yinon Ehrlich 16 years, 7 months ago  # | flag

Alternate way to do the same thing. What about this ?

suite = unittest.TestLoader().loadTestsFromTestCase( TestClassName )
unittest.TextTestRunner(verbosity=2).run(suite)

IMHO it's more elagant...

Michael Lieberman 16 years ago  # | flag

Another way to avoid modifying sys.argv.

unittest.main(argv = unittest.sys.argv + ['--verbose'])