Jakub M.
Jakub M.

Reputation: 33857

unittest: increase module's verbosity when tested

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

Answers (3)

GilZ
GilZ

Reputation: 6477

You can call set_verbose from the setUp method of the unittest.

Upvotes: 3

Robert
Robert

Reputation: 32785

if __name__ == '__main__':
    unittest.main(verbosity=2)

see: https://docs.python.org/2/library/unittest.html

Upvotes: 12

user_noname_00
user_noname_00

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

Related Questions