python config_error ai_generated true

RuntimeError: setUpClass is not called for TestCase subclass because it has no __test__ attribute

ID: python/unittest-setupclass-order-error

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.12 active

Root Cause

当测试类名以 'test' 开头但缺少 __test__ 属性时,unittest 将其视为非测试类

generic

中文

当测试类名以 'test' 开头但缺少 __test__ 属性时,unittest 将其视为非测试类

Workarounds

  1. 90% success
    确保类继承自 unittest.TestCase,并命名以 test 开头
  2. 70% success
    自定义 loader 加载测试类,指定 loadTestsFromTestCase

Dead Ends

Common approaches that don't work:

  1. 40% fail

    unittest 框架仍会因其他原因跳过该类

  2. 60% fail

    如果类没有正确继承 TestCase,仍会失败