python runtime_error ai_generated true

AssertionError: 0 != 1 : SubTest failed

ID: python/unittest-subtest-failure-not-reported

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

A subTest failure is not being properly reported because the subTest block doesn't raise an assertion error correctly.

generic

中文

子测试失败未被正确报告,因为 subTest 块没有正确引发断言错误。

Workarounds

  1. 95% success
    for i in range(5):
        with self.subTest(i=i):
            self.assertEqual(i, 0)
  2. 90% success
    @pytest.mark.parametrize('i', range(5))
    def test_foo(i):
        assert i == 0

Dead Ends

Common approaches that don't work:

  1. 70% fail

    This loses granularity of test reporting.

  2. 80% fail

    This may swallow the exception and not mark the test as failed.