Repository navigation
Replies: 4 comments
|
From the docs and the older issues, I would not treat this as expected behavior for the basic case. As I understand it:
So this should work: import logging
def test_debug_logging(caplog):
caplog.set_level(logging.DEBUG)
logging.debug("debug message")
assert "debug message" in caplog.textIf this minimal example fails on pytest 9.0.2 with If the minimal example works but your real project does not, I would check two common logging gotchas First, make sure the root logger handlers are not being replaced during the test or app initialization. The Second, check whether your application logger has import logging
logger = logging.getLogger("myapp")
def test_debug_logging(caplog):
caplog.set_level(logging.DEBUG, logger="myapp")
logger.debug("debug message")
assert "debug message" in caplog.textSo the short answer is: the basic behavior you describe should not be expected after #7340. If a minimal root-logger repro fails, it is likely a regression. If only your project fails, I would first look for logging reconfiguration or non-propagating named loggers |
|
The basic case was checked on pytest 9.0.2 with There is one detail in the existing suggestion worth separating: targeting a named logger with If non-propagation is intentional, you can temporarily attach caplog's capture handler to that logger for the test: logger = logging.getLogger("myapp")
logger.addHandler(caplog.handler)
try:
with caplog.at_level(logging.DEBUG, logger="myapp"):
logger.debug("captured directly")
finally:
logger.removeHandler(caplog.handler)
assert "captured directly" in caplog.textThis example assumes the capture handler is not already attached to that named logger. It keeps All three checks passed locally: root DEBUG with ini INFO; named non-propagating DEBUG absent after |
|
According to the pytest logging documentation and the fix in #7340, I would first try a minimal reproduction: import logging
def test_debug_capture(caplog):
caplog.set_level(logging.DEBUG)
logging.debug("debug message")
assert "debug message" in caplog.textRun it with If that passes, the problem may be related to your application's logger configuration rather than the global pytest setting. In particular:
For a named logger, try: caplog.set_level(logging.DEBUG, logger="myapp")If the minimal reproduction itself fails on pytest 9.0.2, that would be useful evidence of a possible regression. In that case, sharing the minimal test, configuration, and pytest version would help maintainers investigate. Reference: https://docs.pytest.org/en/stable/how-to/logging.html |
|
Does the DEBUG message show up in To include DEBUG in that section, run the test with |
Uh oh!
There was an error while loading. Please reload this page.
I see there are a number of issues on the tracker around this, both open and closed, with some closed ones implying this behaviour should no longer be occurring. I'd like to get some clarity on the current status of things and any expected changes coming up.
I am currently using pytest 9.0.2.
Currently if, for example,
log_levelin the ini file is set toINFOand a test callscaplog.set_level(logging.DEBUG)(or uses the equivalentat_levelcontext manager), this does not log any messages at theDEBUGlevel during the test. The globalINFOsetting still applies.When I went looking for answers on this I found #7133, which looked like this exact problem and seemed to have already been solved in pull request #7340. Unless I'm missing something, I still have the same issue though. I see there are other open issues such as #7162 which seem to be talking about the same/a similar thing, but as I dig around and find more of them they get a bit further into the pytest internals than I'm familiar with as a user rather than a developer of the package.
Is what I'm seeing expected behaviour, or did the bug from 6 years ago rear its head again at some point?
All reactions