python runtime_error ai_generated true

断言错误:在测试方法之前未调用 setUpClass

AssertionError: setUpClass not called before test methods

ID: python/unittest-setupclass-not-called

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-08-30首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

测试类未继承 unittest.TestCase,或者 setUpClass 定义错误(例如,作为实例方法)。

English

The test class does not inherit from unittest.TestCase, or setUpClass is defined incorrectly (e.g., as an instance method).

generic

解决方案

  1. 99% 成功率
    class TestMyClass(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            ...
  2. 90% 成功率
    @pytest.fixture(scope='class')
    def setup():
        ...

无效尝试

常见但无效的做法:

  1. 70% 失败

    setUp is called for each test, not once for the class.

  2. 50% 失败

    If the class doesn't inherit from TestCase, it won't be recognized.