I am writing a small Python flask app whose sole purpose is to make a request to get a web page at one specific URL (always the same) and parse a section of the content. The app surfaces two endpoints, /games/ and /games/. If the former, it returns info for all games listed on the web page, if the latter, just for that one game.
I have been asked to write unit tests and treat the web page as the data, but I dont think the test should hit the target URL through the network, so I want to make it so the web page is read from a file that I produced with curl and use that as the source.
What I am struggling with is how to insert the data into the response object from a requests.get() call. I've gone through pages and pages of examples of how to do mocking, but there isn't a clear, simple explanation of how to do this.
That's all I need to do. I have a file with the html from the original web site and I want the response data to come from the file, not the web site.
Any ideas?
UPDATE:
I figured it out on my own:
I separated out the line that calls requests.get into a function get_html then I mocked it in the test app.
@mock.patch ('myflaskApp.get_html')
def test_get_html(get_html):
theSession = requests.Session()
theSession.mount('file://', FileAdapter())
resp = theSession.get('file://'+os.getcwd()+'/mytestdata.html')
And then I simplified things even more by realizing I don't need to mock anything. I changed get_html() to check if the app.testing flag is true and have it load the adapter that way otherwise do a regular get.