# AssertionError: assert '' == 'hello\n'
  - 
  + hello

- **ID:** `python/pytest-capsys-flush-buffering`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

`capsys.readouterr()` 返回空，因为被测代码使用了 `print(..., flush=False)` 且缓冲区尚未刷新，或者它写入的是 `sys.__stdout__` 而不是 `sys.stdout`。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Force flush in the code under test or use print(..., flush=True):

print('hello', flush=True)
   ```
2. **** (90% 成功率)
   ```
   Use capfd and ensure the code writes to sys.stdout:

def test_out(capfd):
    my_func()
    out, err = capfd.readouterr()
    assert out == 'hello\n'
   ```

## 无效尝试

- **** — Flushing is not time-based; the buffer may still be unflushed and the test is now flaky. (80% 失败率)
- **** — capfd captures file descriptors; if the code uses sys.__stdout__ directly it still bypasses both. (60% 失败率)
