python runtime_error ai_generated true

FAILED tests/test_x.py::TestX::test_y - 子测试失败 # 但退出码为 0 且 CI 通过

FAILED tests/test_x.py::TestX::test_y - subtest failed # but exit code is 0 and CI passes

ID: python/pytest-subtests-failure-not-counted

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

版本兼容性

版本状态引入弃用备注
7.x active
8.x active

根因分析

unittest subTest 失败会被报告,但如果测试方法本身未传播失败,pytest 的退出码可能仍为 0,尤其在较旧 pytest 或吞掉子测试结果的插件中。

English

unittest subTest failures are reported but pytest's exit code may remain 0 if the test method itself doesn't propagate the failure, especially in older pytest or with plugins that swallow subtest outcomes.

generic

解决方案

  1. 95% 成功率
    pip install -U pytest
    # pytest >= 7.2 correctly reports subtest failures in exit code
  2. 85% 成功率
    def test_y(self):
        errors = []
        for case in cases:
            with self.subTest(case=case):
                try:
                    check(case)
                except AssertionError as e:
                    errors.append(e)
        if errors:
            raise AssertionError(errors)

无效尝试

常见但无效的做法:

  1. 85% 失败

    Stops on first failure but doesn't change exit code semantics for subtest failures.

  2. 95% 失败

    Changes traceback style, not exit code behavior.

  3. 70% 失败

    Loses the per-subtest granularity that subTest was meant to provide.