python
runtime_error
ai_generated
true
AssertionError: 1 != 2 : 测试结果依赖于执行顺序
AssertionError: 1 != 2 : Test results depend on execution order
ID: python/unittest-testcase-ordering-issue
80%修复率
85%置信度
0证据数
2025-03-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
测试共享可变状态(例如全局变量或类属性)而没有适当的隔离,导致顺序依赖的失败。
English
Tests share mutable state (e.g., a global variable or class attribute) without proper isolation, causing order-dependent failures.
解决方案
-
95% 成功率
Use setUp() to initialize fresh state for each test: `def setUp(self): self.shared_var = 0`
-
90% 成功率
Refactor tests to avoid shared state by using local variables or mocks.
无效尝试
常见但无效的做法:
-
90% 失败
This is a brittle workaround; any change in test discovery order can break tests again.
-
80% 失败
setUpClass runs once per class, not per test, so state can still leak between tests.