python runtime_error ai_generated true

断言错误:0 != 1:子测试失败

AssertionError: 0 != 1 : SubTest failed

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

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

版本兼容性

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

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 70% 失败

    This loses granularity of test reporting.

  2. 80% 失败

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