Question
I have a function that calls a url and then modifies that url’s response. How can I write a unit test for the portion of that function’s code that modifies the response, without having to rely on that url?
Example
my_module.py
import requests
def get_some_resource():
url = 'http://httpbin.org/get'
r = requests.get(url)
# Special manipulation of returned text (what I want to test) simple example used
output = r.text.upper()
return output
What I’ve tried so far
- Using mock’s MagicMock() (you can’t use it to override a function’s variables, as far as I can tell)
- I’ve considered breaking apart the two sections of that function (the retrieval of the url and the modify of the response), however, I’m not clear if that’s necessary
- So much googling my hands hurt
If this is a unit test, you would just probably want to mock out the
requestslibrary. You can either justpatchthe whole thing, or justget, it doesn’t really matter.It’d look like:
Cheers.