python
runtime_error
ai_generated
true
断言错误:0 != 1:子测试失败
AssertionError: 0 != 1 : SubTest failed
ID: python/unittest-subtest-failure-not-reported
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.
解决方案
-
95% 成功率
for i in range(5): with self.subTest(i=i): self.assertEqual(i, 0) -
90% 成功率
@pytest.mark.parametrize('i', range(5)) def test_foo(i): assert i == 0
无效尝试
常见但无效的做法:
-
70% 失败
This loses granularity of test reporting.
-
80% 失败
This may swallow the exception and not mark the test as failed.