python
runtime_error
ai_generated
true
AssertionError: <class 'ValueError'> 未引发
AssertionError: <class 'ValueError'> not raised
ID: python/unittest-assertraises-wrong-exception
80%修复率
89%置信度
0证据数
2024-01-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
被测代码未引发预期的异常。这可能是因为代码路径未触发、异常类型不同,或代码静默处理了错误。
English
The code under test did not raise the expected exception. This could be because the code path was not triggered, the exception type is different, or the code silently handles the error.
解决方案
-
92% 成功率
with self.assertRaises(ValueError) as cm: my_function(invalid_input) self.assertEqual(str(cm.exception), 'expected message') -
95% 成功率
import pytest with pytest.raises(ValueError, match='expected message'): my_function(invalid_input)
无效尝试
常见但无效的做法:
-
80% 失败
This broadens the assertion and may pass even when the wrong exception is raised, hiding bugs.
-
85% 失败
This bypasses assertRaises entirely and does not verify the exception type correctly.