python config_error ai_generated true

断言错误:断言 '' 在 'Some stderr output' 中

AssertionError: assert '' in 'Some stderr output'

ID: python/pytest-capsys-stderr-not-captured

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2025-10-10首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

capsys fixture 默认捕获 stdout 但不捕获 stderr,或者测试错误地使用了 print 到 stderr。

English

The capsys fixture captures stdout but not stderr by default, or the test is using print to stderr incorrectly.

generic

解决方案

  1. 95% 成功率
    captured = capsys.readouterr()
    assert 'expected' in captured.err
  2. 90% 成功率
    def test_foo(capfd):
        print('stderr', file=sys.stderr)
        captured = capfd.readouterr()
        assert 'stderr' in captured.err

无效尝试

常见但无效的做法:

  1. 60% 失败

    capfd captures file descriptors, which may not be what you want.

  2. 80% 失败

    This doesn't affect capture.