# 超时错误：测试在 30 秒后超时

- **ID:** `python/pytest-timeout-test-hang`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

测试花费的时间超过指定的超时时间，通常是由于无限循环、慢速 I/O 或阻塞调用。

## 版本兼容性

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

## 解决方案

1. **Add debugging output to identify where the test hangs.** (85% 成功率)
   ```
   Insert print statements or use logging to trace execution.
   ```
2. **Use pytest-timeout plugin to set a timeout and catch hanging tests.** (90% 成功率)
   ```
   pytest --timeout=60 test_file.py
   ```

## 无效尝试

- **Increasing the timeout value without investigating the cause** — The underlying issue (e.g., infinite loop) will cause the test to hang indefinitely. (70% 失败率)
- **Removing the timeout decorator to avoid failure** — The test may still hang indefinitely, blocking the test suite. (80% 失败率)
