python
runtime_error
ai_generated
true
AssertionError: 1 != 2 : Test results depend on execution order
ID: python/unittest-testcase-ordering-issue
80%Fix Rate
85%Confidence
0Evidence
2025-03-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
Tests share mutable state (e.g., a global variable or class attribute) without proper isolation, causing order-dependent failures.
generic中文
测试共享可变状态(例如全局变量或类属性)而没有适当的隔离,导致顺序依赖的失败。
Workarounds
-
95% success
Use setUp() to initialize fresh state for each test: `def setUp(self): self.shared_var = 0`
-
90% success
Refactor tests to avoid shared state by using local variables or mocks.
Dead Ends
Common approaches that don't work:
-
90% fail
This is a brittle workaround; any change in test discovery order can break tests again.
-
80% fail
setUpClass runs once per class, not per test, so state can still leak between tests.