python runtime_error ai_generated true

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

ID: python/pytest-caplog-empty-handler

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-07-14首次发现

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 60% 失败

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

  2. 80% 失败

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