python runtime_error ai_generated true

test_something (tests.test_x.TestX.test_something) ... ok # but the test body contains: assertEqual(1, 2) and never runs

ID: python/unittest-assertion-always-true-no-self

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.12 active — — —

Root Cause

A helper method named like a test (test_*) inside the TestCase lacks the `self` parameter or is defined as a staticmethod, so unittest treats it as a test but it fails silently or is skipped.

generic

中文

TestCase 中名为 test_* 的辅助方法缺少 `self` 参数或被定义为 staticmethod,unittest 将其当作测试但静默失败或被跳过。

Workarounds

  1. 95% success
    class TestX(unittest.TestCase):
        def _build_payload(self):
            return {'k': 1}
    
        def test_payload(self):
            self.assertEqual(self._build_payload(), {'k': 1})
  2. 90% success
    def build_payload():
        return {'k': 1}
    
    class TestX(unittest.TestCase):
        def test_payload(self):
            self.assertEqual(build_payload(), {'k': 1})

Dead Ends

Common approaches that don't work:

  1. 40% fail

    Renaming fixes discovery but does not explain why the assertions were silently skipped; other tests may still be misnamed.

  2. 70% fail

    Hides the real issue; the method is still collected as a test and reported as skipped, polluting results.