python
runtime_error
ai_generated
true
AssertionError: Exception not raised
ID: python/assertionerror-assert-raises
80%Fix Rate
88%Confidence
0Evidence
2024-12-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
Root Cause
A test expects an exception to be raised using pytest.raises or assertRaises, but the code executes without exception.
generic中文
测试期望使用 pytest.raises 或 assertRaises 引发异常,但代码执行时未引发异常。
Workarounds
-
95% success Use pytest.raises with exact exception
with pytest.raises(ValueError): raise ValueError('test') -
90% success Use unittest.TestCase.assertRaises
self.assertRaises(ValueError, func, arg)
Dead Ends
Common approaches that don't work:
-
Adding a try-except block manually
60% fail
Manual try-except may miss the exact exception type or not fail properly.
-
Using a broader exception type
50% fail
Catches unintended exceptions and may hide bugs.