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

1

8

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 ?

Kneepan answered 24/3, 2017 at 14:14 Comment(4)
Could you use the logging module and just create a logger for your tests? I think you can pull the logging level from the logger if you need that for something else.Turbulence
@FamousJameous: I have not looked into using the logging module; thought that the verbosity information may be immediately available.Kneepan
I mean, you could certainly do this manually, you would just parse the command line and forward that information to your tests. But if the reason you want the verbosity level is for deciding whether to print a message or not, use the logging module.Turbulence
@FamousJameous, how could I use the logging to print a message accordingly to the verbosity level as you suggest?Timmi
B
7

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.

Beelzebub answered 24/3, 2017 at 14:40 Comment(1)
Thanks, so I will just do ('-v' in argv) or ('--verbose' in argv).Kneepan

© 2022 - 2024 — McMap. All rights reserved.