python
config_error
ai_generated
true
AssertionError: assert 'user created' in '' + 其中 '' = caplog.text
AssertionError: assert 'user created' in '' + where '' = caplog.text
ID: python/pytest-caplog-no-records
80%修复率
84%置信度
0证据数
2024-05-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
caplog 通过根 logger 处理器捕获记录,但应用 logger 设置了 `propagate = False`、自定义处理器及级别,或日志级别高于所发出的消息。
English
caplog captures records via the root logger handler, but the application logger has `propagate = False`, a custom handler with its own level, or the log level is higher than the emitted message.
解决方案
-
90% 成功率
Force propagation and set level on the specific logger: def test_create(caplog): caplog.set_level(logging.INFO, logger='myapp.users') create_user('alice') assert 'user created' in caplog.text -
82% 成功率
If propagate is intentionally False, attach caplog's handler: logger = logging.getLogger('myapp.users') logger.addHandler(caplog.handler) caplog.set_level(logging.INFO)
无效尝试
常见但无效的做法:
-
70% 失败
If propagate=False, records never reach caplog's handler regardless of level.
-
85% 失败
logger.info returns None; printing does not capture anything and the assertion still fails.