python
runtime_error
ai_generated
true
AssertionError: ValueError not raised
ID: python/unittest-assertraises-no-exception
80%Fix Rate
88%Confidence
0Evidence
2024-02-27First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
self.assertRaises(ValueError) was used as a context manager but the code under test did not raise, or the exception was caught and swallowed inside the function.
generic中文
self.assertRaises(ValueError) 作为上下文管理器使用,但被测代码未引发异常,或异常在函数内部被捕获并吞掉。
Workarounds
-
90% success
with self.assertRaises(ValueError) as ctx: parse('bad-input') self.assertIn('bad-input', str(ctx.exception)) -
92% success
import pytest def test_parse_raises(): with pytest.raises(ValueError, match=r'bad-input'): parse('bad-input')
Dead Ends
Common approaches that don't work:
-
85% fail
Inverts the logic: except catches the expected exception and calls fail, so a correct raise now fails the test.
-
75% fail
Broadens the assertion so much that any unrelated error (e.g. TypeError) passes the test, masking real bugs.