python
config_error
ai_generated
true
AssertionError: assert 'processing item' in '' # caplog.text is empty
ID: python/pytest-caplog-propagation-disabled
80%Fix Rate
86%Confidence
0Evidence
2024-09-27First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
The application logger has propagate=False set (often to avoid duplicate handlers), so records never reach the root logger that caplog attaches its handler to. caplog captures nothing.
generic中文
应用 logger 设置了 propagate=False(通常用于避免重复 handler),因此日志记录永远到不了 caplog 挂载 handler 的根 logger。caplog 捕获不到任何内容。
Workarounds
-
92% success
def test_logs(caplog): logger = logging.getLogger('myapp') logger.propagate = True with caplog.at_level(logging.INFO, logger='myapp'): run() assert 'processing item' in caplog.text -
88% success
def test_logs(caplog): logger = logging.getLogger('myapp') logger.addHandler(caplog.handler) try: run() finally: logger.removeHandler(caplog.handler) assert any('processing item' in r.message for r in caplog.records)
Dead Ends
Common approaches that don't work:
-
80% fail
Level is not the problem; propagation is disabled so no records reach caplog regardless of level.
-
70% fail
Logging handlers may not write to stderr, and the format is not the log message alone.