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

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-12-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 95% success
    def test_start(caplog):
        with caplog.at_level("INFO"):
            start()
        assert "starting" in caplog.text
  2. 85% success
    def test_start(capsys):
        start()
        captured = capsys.readouterr()
        assert "starting" in captured.err

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Nothing was written to stdout in the first place.

  2. 90% fail

    Same stream issue; binary vs text doesn't help.

  3. 80% fail

    Pollutes production code to satisfy a test.