Reputation: 33857
I have a module mymodule
that I test with unittest
. The module logs to stdout diagnostics messages when is in verbose mode (e.g. mymodule.set_verbose(True)
) and remains silent otherwise. I would like that when I import the module in the main program it is in the silent mode and when the unittest
runs, it is verbose.
I tried to hack it in the unittest main loop, but it doesn't work
if __name__ == "__main__":
mymodule.set_verbose( True )
unittest.main()
# apparently, modules are loaded on each test separately
How to increase verbosity in python unittest? was not helpful.
Upvotes: 7
Views: 12485
Reputation: 32785
if __name__ == '__main__':
unittest.main(verbosity=2)
see: https://docs.python.org/2/library/unittest.html
Upvotes: 12
Reputation: 369
Alternatively you could directly use unittest.TextTestRunner
to run your tests. This allows to set the verbosity level:
suite = unittest.TestLoader().loadTestsFromTestCase(TestCaseClass)
unittest.TextTestRunner(verbosity=2).run(suite)
This will run all your tests from within TestCaseClass
.
Upvotes: 6