python runtime_error ai_generated true

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

ID: python/pytest-subtests-not-counted

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2025-05-22首次发现

版本兼容性

版本状态引入弃用备注
3.10 active — — —
3.12 active — — —

根因分析

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

English

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

解决方案

  1. 98% 成功率
    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% 成功率
    Install pytest-subtests to surface each subTest as its own result:
    
    pip install pytest-subtests
    
    # now each subTest failure is reported individually

无效尝试

常见但无效的做法:

  1. 30% 失败

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

  2. 90% 失败

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