Is it possible to access "real" objects while mocking a module? What I'm trying to do is mock some function, but throw "real" exceptions, like this:
@mock.patch('my_project.requests') def test_foo(self, mock_requests): mock_requests.post = mock.Mock(side_effect=requests.ConnectionError()) thread = CommandThread("command", 3, 2, 0) thread.run() #this is were I exercise requests.post self.assert(thread.was_successful is False)
Inside my CommandThread I have a check like
try: requests.post(url, data=data) except (requests.ConnectionError, requests.Timeout): self.was_successful = False
however, my test fails because the exception is not caught inside the try/except block (when I do like
except Exception: it works) The reason, I think, is simply because I mocked this "namespace" in my test case, so I actually raise
my_project.requests.ConnectionError exception rather than proper,
requests.ConnectionError from original package. Is it somehow possible to access/throw "real" exceptions?