python
runtime_error
ai_generated
partial
FAILED (failures=0) # 但打印了 subTest 失败: FAIL: test_batch (tests.test_batch.TestBatch.test_batch) (i=3) AssertionError: 3 != 4
FAILED (failures=0) # but subTest failures printed: FAIL: test_batch (tests.test_batch.TestBatch.test_batch) (i=3) AssertionError: 3 != 4
ID: python/unittest-subtest-failure-swallowed
80%修复率
80%置信度
0证据数
2025-05-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
subTest 失败记录在父测试上,但某些运行器或自定义结果类不会增加 failures 计数器,导致摘要显示 failures=0 而个别子测试实际失败。
English
subTest failures are recorded against the parent test but some runners or custom result classes do not increment the failures counter, so the summary reports failures=0 even though individual subtests failed.
解决方案
-
90% 成功率
Run with a result class that counts subtests, or use pytest which reports them: python -m pytest tests/test_batch.py -v
-
85% 成功率
Inspect result.failures + result.errors + subtest count in a custom runner: result = unittest.TextTestRunner().run(suite) assert not result.failures and not result.errors
无效尝试
常见但无效的做法:
-
85% 失败
Defeats the purpose of subTest (continue-on-failure) and stops at the first failing case.
-
90% 失败
The process may still exit 0 when failures counter is zero, so CI passes a broken suite.