python
type_error
ai_generated
true
AssertionError: ValueError not raised During handling of the above exception, another exception occurred: TypeError: 'ValueError' object is not callable
ID: python/unittest-mock-side-effect-not-raised
80%Fix Rate
90%Confidence
0Evidence
2024-02-28First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
mock.side_effect was set to an exception instance instead of the exception class; mock tries to call the instance and raises TypeError instead of the intended exception.
generic中文
mock.side_effect 被设置为异常实例而不是异常类;mock 尝试调用该实例并抛出 TypeError,而非预期的异常。
Workarounds
-
96% success
from unittest import mock with mock.patch('myapp.fetch') as m: m.side_effect = ValueError # class, not ValueError() with pytest.raises(ValueError): call_sut() -
94% success
def boom(*args, **kwargs): raise ValueError('bad input') with mock.patch('myapp.fetch', side_effect=boom): with pytest.raises(ValueError, match='bad input'): call_sut()
Dead Ends
Common approaches that don't work:
-
90% fail
return_value returns the exception object as the call result; it is never raised, so the test still fails.
-
85% fail
Masks the mock misconfiguration and makes the test pass for the wrong reason; the SUT never actually raised.