python
runtime_error
ai_generated
true
assert 0 == 1 + where 0 = len(caplog.records)
ID: python/pytest-caplog-empty-handler
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.
解决方案
-
90% 成功率
`caplog.set_level(logging.INFO, logger='mylogger')` and ensure `logging.getLogger('mylogger').propagate = True` -
85% 成功率
with caplog.at_level(logging.INFO): myfunc() assert 'expected' in caplog.text
无效尝试
常见但无效的做法:
-
60% 失败
If the logger has `propagate = False`, records never reach the root handler that caplog attaches to.
-
80% 失败
Loses structured logging and doesn't test the actual logging behavior.