# AssertionError: assert 'foo' == 'bar'  # 间歇性

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

## 根因

测试依赖于顺序或共享可变状态；当以不同顺序运行（例如使用 `-p no:randomly` 或 xdist）时，它会失败。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   @pytest.fixture
def state():
    return {}

def test_a(state):
    state['x'] = 1
   ```
2. **** (85% 成功率)
   ```
   pytest -p randomly --randomly-seed=12345
   ```

## 无效尝试

- **** — Masks the underlying bug and increases test time; failures may still occur. (70% 失败率)
- **** — Hides the order dependency but does not fix it; CI may use a different order. (60% 失败率)
