A while back I found the following solution-pattern here on StackOverflow for raising an error from a mocked function (I cannot find the original link anymore, sorry):
def _mock_raises_error(my_mock, error_type, output):
my_mock.return_value = mock.Mock()
my_mock.side_effect = error_type(output)
# for example
with mock.patch('mymodule.interface.function') as mock_function:
_mock_raises_error(mock_function, Exception, 'Some error-message')
This works as intended, so I always have used this pattern.
Recently a colleague of mine asked why this definition both has a return_value as a side_effect and why that was needed. And to my shame I could not come up with the correct answer (only that I copied it from StackOverflow, but that is not an explanation why it is correct).
So now my question (to be able to explain it in the future) is why isn't giving the side_effect enough? What does the return_value add?