Search code examples
pythonmockingpython-mock

Exception not called when using side_effect with mock


I have a function in a class called "my_class" in a module called "my_module" that contains this snippet:

try:
  response = self.make_request_response(requests.post, data, endpoint_path)
except requests.exceptions.HTTPError as err:
  if err.response.status_code == requests.codes.conflict:
    logging.info('Conflict error')

And I'm trying to test it like so:

error = requests.exceptions.HTTPError(mock.Mock(response=mock.Mock(status_code=409)), 'not found')
mock_bad = mock.Mock(side_effect=error)
mock_good = mock.Mock()
mock_good.return_value = [{'name': 'foo', 'id': 1}]


upsert = my_module.my_class(some_data)
with mock.patch.object(upsert, 'make_request_response', side_effect=[mock_bad, mock_good]) as mock_response:
    some_function()

What I would expect is for an HTTPError to be raised in the test after I patch it. However, when I run the test, an exception is never raised. "response" is actually set to mock_bad, which contains the desired exception, although it's never raised. Any idea where I'm going wrong here?


Solution

  • You put your exception into the wrong side effect. Calling make_request_response() now first returns the mock_bad mock, which by itself won't raise that exception until called.

    Put the exception in the mock.patch.object() side_effect list:

    error = requests.exceptions.HTTPError(mock.Mock(response=mock.Mock(status_code=409)), 'not found')
    mock_good = mock.Mock()
    mock_good.return_value = [{'name': 'foo', 'id': 1}]
    
    
    upsert = my_module.my_class(some_data)
    with mock.patch.object(upsert, 'make_request_response', side_effect=[error, mock_good]) as mock_response:
        some_function()