python runtime_error ai_generated true

FAILED tests/test_math.py::test_ops - AssertionError: 1 != 2

ID: python/pytest-subtests-not-counted

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.10 active — — —
3.12 active — — —

Root Cause

When using `self.subTest(...)` inside a unittest.TestCase, subtests do not stop the parent test from continuing, but the parent still reports as FAILED. Developers expect each subtest to appear as a separate test case.

generic

中文

在 unittest.TestCase 内使用 `self.subTest(...)` 时,子测试不会阻止父测试继续执行,但父测试仍报告为 FAILED。开发者期望每个子测试作为独立的测试用例出现。

Workarounds

  1. 98% success
    Use pytest's parametrize instead for per-case reporting:
    
    import pytest
    
    @pytest.mark.parametrize('a,b,expected', [(1,1,2),(2,2,4)])
    def test_add(a, b, expected):
        assert a + b == expected
  2. 90% success
    Install pytest-subtests to surface each subTest as its own result:
    
    pip install pytest-subtests
    
    # now each subTest failure is reported individually

Dead Ends

Common approaches that don't work:

  1. 30% fail

    Loses the loop-based data-driven style; verbose but works, so it is not really a dead end—more of a workaround.

  2. 90% fail

    Raises immediately, aborting remaining subtests and defeating the purpose of subTest.