python runtime_error ai_generated true

AssertionError: setUpClass not called before test methods

ID: python/unittest-setupclass-not-called

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-08-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 50% fail

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