A "global" log level set by log_level cannot be overridden at the test level.
Minimal Example:
# setup.cfg
[tool:pytest]
log_level = CRITICAL
# test_example.py
import logging
def function_that_logs():
logging.debug('message')
def test_logger(caplog):
caplog.set_level(logging.DEBUG)
function_that_logs()
assert caplog.text
Running pytest-master@077d1c3, Python 3.7.6.
It looks like this is because log_level adjusts the level of LogCaptureHandler() in _runtest_for_main() whereas caplog.set_level() adjusts the level of a specific Logger (likely Root).
If I add self.handler.setLevel(level) to the end of LogCaptureFixture.set_level() the above example passes. Is this a suitable fix (assuming the corresponding cleanup is added too)? Or is this a misuse of log_level?
A "global" log level set by
log_levelcannot be overridden at the test level.Minimal Example:
Running
pytest-master@077d1c3, Python 3.7.6.It looks like this is because
log_leveladjusts the level ofLogCaptureHandler()in_runtest_for_main()whereascaplog.set_level()adjusts the level of a specificLogger(likelyRoot).If I add
self.handler.setLevel(level)to the end ofLogCaptureFixture.set_level()the above example passes. Is this a suitable fix (assuming the corresponding cleanup is added too)? Or is this a misuse oflog_level?