python
runtime_error
ai_generated
true
AssertionError: ValueError not raised
ID: python/unittest-assertraises-not-raised
80%Fix Rate
85%Confidence
0Evidence
2024-06-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
The code under test does not raise the expected exception, or the exception is caught inside the function, or the test is not actually executing the code that should raise.
generic中文
被测代码没有引发预期的异常,或者异常在函数内部被捕获,或者测试实际上没有执行应该引发异常的代码。
Workarounds
-
85% success
Add print statements before the call to ensure it's reached. Use pdb.set_trace() to step through. Check if the function has a try/except that swallows the error.
-
90% success
with self.assertRaises(ValueError) as cm: my_func() print(cm.exception) # see if any exception was raised
Dead Ends
Common approaches that don't work:
-
90% fail
This duplicates assertRaises and often leads to false positives if the exception is caught.
-
95% fail
The underlying issue is that the exception is not raised, so switching frameworks won't help.