python
runtime_error
ai_generated
true
AssertionError: assert 'starting' in capsys.readouterr().out E AssertionError: assert 'starting' in ''
ID: python/pytest-capture-log-with-caplog-and-capsys
80%Fix Rate
87%Confidence
0Evidence
2024-12-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
The message was emitted via logging, not print, so capsys captured nothing. Logging writes to stderr through the logging handler, not stdout.
generic中文
该消息通过 logging 发出,而不是 print,因此 capsys 没有捕获到任何内容。日志记录通过日志处理器写入 stderr,而不是 stdout。
Workarounds
-
95% success
def test_start(caplog): with caplog.at_level("INFO"): start() assert "starting" in caplog.text -
85% success
def test_start(capsys): start() captured = capsys.readouterr() assert "starting" in captured.err
Dead Ends
Common approaches that don't work:
-
90% fail
Nothing was written to stdout in the first place.
-
90% fail
Same stream issue; binary vs text doesn't help.
-
80% fail
Pollutes production code to satisfy a test.