python
runtime_error
ai_generated
true
AssertionError: assert 'expected message' in []
ID: python/pytest-caplog-empty
80%Fix Rate
86%Confidence
0Evidence
2024-07-19First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
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).
generic中文
caplog 未捕获任何记录,因为日志传播被禁用、日志记录器级别过滤了消息,或者日志记录调用发生在安装 caplog 之前(例如在导入时)。
Workarounds
-
95% success
def test_logs(caplog): import logging caplog.set_level(logging.INFO, logger="app") do_work() assert "expected message" in caplog.text -
93% success
def test_logs(caplog): with caplog.at_level(logging.DEBUG, logger="app"): do_work() assert "expected message" in caplog.text -
88% success
logger = logging.getLogger("app") logger.propagate = True # do not disable in tests
Dead Ends
Common approaches that don't work:
-
90% fail
Level must be set before the log call to capture it.
-
80% fail
Defeats the purpose of testing logs and doesn't fix capture.
-
95% fail
clear() empties the buffer; it doesn't populate it.