# 类型错误：需要类似字节的对象，而不是 'str'，在 capsys.readouterr() 中

- **ID:** `python/pytest-capture-output-binary`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用 capsys（捕获文本）来捕获来自 sys.stdout.buffer 的二进制输出，当代码写入字节时导致类型不匹配。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.11 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Use capfd (capture file descriptors) instead of capsys to capture both text and binary output: out, err = capfd.readouterr()
   ```
2. **** (85% 成功率)
   ```
   If the output is binary, use sys.stdout.buffer.write() and capture with capfd, then decode as needed.
   ```

## 无效尝试

- **** — Using capfd instead of capsys may work for binary but fails if the test also needs to capture text from sys.stdout. (50% 失败率)
- **** — Decoding bytes to str before assertion may mask encoding issues in the output. (40% 失败率)
