python config_error ai_generated true

Logging output not captured: Captured logs are not displayed in the test report even with -s flag

ID: python/pytest-capture-logging-not-showing

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-04-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Logging is configured at the root level without a handler that pytest can capture, or the caplog fixture is not used.

generic

中文

日志在根级别配置,没有 pytest 可以捕获的处理程序,或者未使用 caplog fixture。

Workarounds

  1. 95% success
    def test_logging(caplog):
        with caplog.at_level(logging.DEBUG):
            function_that_logs()
        assert 'Expected log' in caplog.text
  2. 90% success
    [pytest]
    log_cli = true
    log_level = DEBUG

Dead Ends

Common approaches that don't work:

  1. 70% fail

    This configures logging globally but may not integrate with pytest's capture mechanism.

  2. 80% fail

    Print statements are captured, but logging is not, so the problem remains.