python runtime_error ai_generated true

assert 0 == 1 + where 0 = len(caplog.records)

ID: python/pytest-caplog-empty-handler

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-07-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

caplog fixture captures logs at WARNING level by default; if the logger propagates=False or the log level is INFO, records are not captured.

generic

中文

caplog fixture 默认捕获 WARNING 级别的日志;如果 logger 的 propagate=False 或日志级别为 INFO,则记录不会被捕获。

Workarounds

  1. 90% success
    `caplog.set_level(logging.INFO, logger='mylogger')` and ensure `logging.getLogger('mylogger').propagate = True`
  2. 85% success
    with caplog.at_level(logging.INFO):
        myfunc()
    assert 'expected' in caplog.text

Dead Ends

Common approaches that don't work:

  1. 60% fail

    If the logger has `propagate = False`, records never reach the root handler that caplog attaches to.

  2. 80% fail

    Loses structured logging and doesn't test the actual logging behavior.