python
runtime_error
ai_generated
true
AssertionError: assert 'hello' in '' 其中 '' = capsys.readouterr().out
AssertionError: assert 'hello' in '' where '' = capsys.readouterr().out
ID: python/pytest-capsys-no-output
80%修复率
88%置信度
0证据数
2024-07-21首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
capsys 仅捕获通过 sys.stdout/sys.stderr 的写入。使用 logging、os.write(1, ...) 或 C 扩展直接写文件描述符 1 的代码会绕过 sys.stdout,无法被捕获。
English
capsys only captures sys.stdout/sys.stderr writes. Code that uses logging, os.write(1, ...), or a C extension writing to fd 1 bypasses Python's sys.stdout and is not captured.
解决方案
-
95% 成功率
Use caplog for logging output: def test_log(caplog): logging.getLogger(__name__).warning('hello') assert 'hello' in caplog.text -
92% 成功率
Use capfd for file-descriptor-level output: def test_fd(capfd): os.write(1, b'hello') out, err = capfd.readouterr() assert 'hello' in out
无效尝试
常见但无效的做法:
-
90% 失败
readouterr() drains the buffer; the second call returns empty strings.
-
85% 失败
Mixing fixtures means output goes to the fd-level buffer while the test reads the sys-level buffer.