python runtime_error ai_generated true

AssertionError: False is not true : expected True

ID: python/unittest-assertion-rewrite-missing

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.10 active
3.11 active
3.12 active

Root Cause

In unittest, `self.assertTrue(x)` on a falsy value produces a generic message; pytest's assertion rewriting does not apply to unittest.TestCase methods, so diffs are opaque.

generic

中文

在 unittest 中,对假值调用 `self.assertTrue(x)` 会生成通用消息;pytest 的断言重写不适用于 unittest.TestCase 方法,因此差异不清晰。

Workarounds

  1. 95% success
    def test_something():
        result = compute()
        assert result == expected  # pytest shows rich diff
  2. 85% success
    self.assertEqual(result, expected)
    self.assertIn(item, container)
    self.assertIsNotNone(obj)

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Disables assertion rewriting entirely, making messages even less informative.

  2. 60% fail

    Adds noise to test output and does not fix the underlying opaque failure; CI logs become harder to scan.

  3. 80% fail

    Global monkeypatch affects all tests and is fragile across unittest versions.