python
config_error
ai_generated
true
AssertionError: assert 'expected message' in [] # caplog.records 为空
AssertionError: assert 'expected message' in [] # caplog.records is empty
ID: python/pytest-caplog-not-propagating
80%修复率
85%置信度
0证据数
2024-09-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
logger 的 propagate 标志为 False,或代码使用了 propagate=False 的自定义 handler 配置的 logger,记录无法到达 caplog 所附加的根 logger。
English
The logger's propagate flag is False, or the code uses a logger configured with a custom handler and propagate=False, so records never reach the root logger that caplog attaches to.
解决方案
-
92% 成功率
def test_logs(caplog): logging.getLogger('mypkg').propagate = True with caplog.at_level(logging.INFO, logger='mypkg'): mypkg.do_work() assert 'expected message' in caplog.text -
85% 成功率
def test_logs(caplog): logger = logging.getLogger('mypkg') logger.addHandler(caplog.handler) mypkg.do_work() assert 'expected message' in caplog.text
无效尝试
常见但无效的做法:
-
70% 失败
Adjusts the level but does not change propagate; the record still never reaches caplog's handler.
-
80% 失败
Adds a root handler but does not affect the child logger's propagate=False setting.