# AssertionError: assert '' == 'expected output'
  - expected output
  + 

- **ID:** `python/pytest-capture-stdout-not-shown`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

测试通过 capsys 捕获 stdout，但被测代码写入的是 logger 或 sys.__stdout__ 而非 sys.stdout，或者 capsys 在输出已被刷新后才被请求。capsys 只捕获调用时通过 sys.stdout 的写入。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (92% 成功率)
   ```
   def test_logs(caplog):
    with caplog.at_level('INFO'):
        run_task()
    assert 'expected output' in caplog.text
   ```
2. **** (90% 成功率)
   ```
   def test_prints(capsys):
    print('expected output')
    captured = capsys.readouterr()
    assert captured.out.strip() == 'expected output'
   ```

## 无效尝试

- **** — capsysbinary only changes the encoding of captured bytes; it still won't capture logger output or writes to sys.__stdout__. (70% 失败率)
- **** — Flushing does not redirect output into the capture buffer if the code wrote to a different stream. (65% 失败率)
