# 检测到不稳定的测试: test_foo 失败 2 次，通过 1 次

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

## 根因

测试不稳定，意味着由于非确定性行为、竞争条件或外部依赖，它有时通过有时失败。

## 版本兼容性

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

## 解决方案

1. **** (85% 成功率)
   ```
   Look for: random data, time-dependent logic, network calls, shared state, order dependence. Use pytest -p no:randomly to disable random order.
   ```
2. **** (90% 成功率)
   ```
   For random: random.seed(42)
For time: freeze_time or mock datetime.now()
For network: use responses or mock.
   ```

## 无效尝试

- **** — This masks the flakiness and can hide real bugs. (90% 失败率)
- **** — This stops the test from running and doesn't fix the underlying issue. (80% 失败率)
