python
runtime_error
ai_generated
true
RuntimeError: tearDownClass() called before setUpClass()
ID: python/unittest-testcase-setup-teardown-error
80%Fix Rate
87%Confidence
0Evidence
2025-01-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
Root Cause
在 unittest.TestCase 中,setUpClass 或 tearDownClass 被错误地调用,或类方法装饰器使用不当。
generic中文
在 unittest.TestCase 中,setUpClass 或 tearDownClass 被错误地调用,或类方法装饰器使用不当。
Workarounds
-
100% success
`@classmethod` 必须在 setUpClass 和 tearDownClass 上,且不能手动调用。
-
90% success
确保父类正确调用 super().setUpClass() 和 super().tearDownClass()。
Dead Ends
Common approaches that don't work:
-
60% fail
可能无法清理资源。
-
80% fail
会重复初始化,且逻辑混乱。