# AssertionError: assert 0 == 1
# 使用 pytest-randomly 时失败，使用 -p no:randomly 时通过

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

## 根因

pytest-randomly 每次运行打乱测试顺序；顺序相关的测试或共享状态导致间歇性失败，这些失败在字母序下被隐藏。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   pytest -p randomly --randomly-seed=12345 tests/ -x
# identify the leak, then fix with autouse reset fixture
   ```
2. **** (92% 成功率)
   ```
   @pytest.fixture(autouse=True)
def reset_globals():
    myapp.state.clear()
    yield
    myapp.state.clear()
   ```

## 无效尝试

- **** — Hides order dependencies and lets bugs ship; the same issue will reappear under xdist or CI parallelism. (90% 失败率)
- **** — Masks the bug for that seed; other seeds still fail and the underlying leak remains. (85% 失败率)
- **** — Requires pytest-ordering and creates brittle coupling; doesn't fix leaked state. (80% 失败率)
