Search code examples
pythonpython-3.xunit-testingverbosity

How can a test in Python unittest get access to the verbosity level?


When calling a test suite in the Python unittest framework, it is possible to give a -v for higher level of verbosity, like:

python3 test_suite.py -v

How can a test case get access to the verbosity level ?


Solution

  • The verbosity isn't directly passed to TestCase or TestSuite since none of them actually need it for something. The runner uses the verbosity information (the class running your tests) to handle the amount of information printed out.

    Looking at the source, since argv isn't cleared at any point, a non-intrusive way to check for the presense of the verbose flag would be to peek into argv and see if '-v' is inside it.