I am testing a Python method which makes an HTTP request to a server. The method looks like this:
def do_something():
data = get_data()
make_request(data)
def make_request(self, data):
self.send_response(200)
self.wfile.write(json.dumps(msg))
From my test, I want to assert based on the data variable. I don't want to touch the production code (i.e., I don't want to make the method return the data explicitly). Is there a way I can mock out the make_request method, but return the params as the return value? Something like:
service.make_request = MagicMock(return_value=params)
If I understand correctly you want to test
get_data()
behavior in your production without change the code.By
mock
framework you can usemake_request(data)
as sensor point andcall_args
to extractdata
(details at calls as tuples).Simple example:
There are too much details to cover here (
patch
decorator,reset_mock()
and so on), but linked documentation should cover all of them.