python
runtime_error
ai_generated
true
AssertionError:assert 'expected message' in []
AssertionError: assert 'expected message' in []
ID: python/pytest-caplog-empty
80%修复率
86%置信度
0证据数
2024-07-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
caplog 未捕获任何记录,因为日志传播被禁用、日志记录器级别过滤了消息,或者日志记录调用发生在安装 caplog 之前(例如在导入时)。
English
caplog captured no records because logger propagation was disabled, the logger level filtered the message, or the logging call happened before caplog was installed (e.g., at import time).
解决方案
-
95% 成功率
def test_logs(caplog): import logging caplog.set_level(logging.INFO, logger="app") do_work() assert "expected message" in caplog.text -
93% 成功率
def test_logs(caplog): with caplog.at_level(logging.DEBUG, logger="app"): do_work() assert "expected message" in caplog.text -
88% 成功率
logger = logging.getLogger("app") logger.propagate = True # do not disable in tests
无效尝试
常见但无效的做法:
-
90% 失败
Level must be set before the log call to capture it.
-
80% 失败
Defeats the purpose of testing logs and doesn't fix capture.
-
95% 失败
clear() empties the buffer; it doesn't populate it.