# 断言错误：测试用例顺序不匹配：预期 test_second 在 test_first 之后

- **ID:** `python/unittest-testcase-order-error`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

依赖执行顺序的测试（例如共享状态）在 unittest 默认按字母顺序运行测试时失败。

## 版本兼容性

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

## 解决方案

1. **Use pytest with pytest-ordering plugin to specify test order explicitly.** (85% 成功率)
   ```
   Install pytest-ordering and use @pytest.mark.run(order=1) decorators.
   ```
2. **Refactor tests to be independent and not rely on order.** (90% 成功率)
   ```
   Use setUp to initialize state for each test individually.
   ```

## 无效尝试

- **Renaming test methods to ensure alphabetical order** — This is fragile and may break if new tests are added with different names. (70% 失败率)
- **Using a global variable to track state across tests** — Shared state can lead to flaky tests and is against testing best practices. (80% 失败率)
