python
config_error
ai_generated
true
AssertionError:assert 'user created' in [] 其中 [] = caplog.messages
AssertionError: assert 'user created' in [] where [] = caplog.messages
ID: python/pytest-caplog-no-logs-captured
80%修复率
86%置信度
0证据数
2024-09-21首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
当根 logger 未配置 handler 时,caplog 默认只捕获 WARNING 及以上级别的记录;或者应用程序使用了 propagate=False 的自定义 logger。必须启用传播才能让 caplog 拦截记录。
English
caplog only captures records at WARNING or above by default when the root logger has no handler configured, or the application uses a custom logger with propagate=False. Propagation must be enabled for caplog to intercept records.
解决方案
-
95% 成功率
def test_create_user(caplog): caplog.set_level(logging.INFO, logger='myapp') create_user('[email protected]') assert 'user created' in caplog.text # in myapp/__init__.py logger = logging.getLogger('myapp') logger.propagate = True -
93% 成功率
def test_create_user(caplog): with caplog.at_level(logging.INFO, logger='myapp'): create_user('[email protected]') assert any('user created' in r.message for r in caplog.records)
无效尝试
常见但无效的做法:
-
90% 失败
Clearing removes whatever was captured; if nothing was captured to begin with, the list stays empty.
-
85% 失败
Logging does not necessarily write to stdout; the logger may use a file or syslog handler, so capsys captures nothing.