This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
mocking requests calls | |
""" | |
import mock | |
import unittest | |
import requests | |
from requests.exceptions import HTTPError | |
def google_query(query): | |
""" | |
trivial function that does a GET request | |
against google, checks the status of the | |
result and returns the raw content | |
""" | |
url = "//www.google.com" | |
params = {'q': query} | |
resp = requests.get(url, params=params) | |
resp.raise_for_status() | |
return resp.content | |
class TestRequestsCall(unittest.TestCase): | |
""" | |
example text that mocks requests.get and | |
returns a mock Response object | |
""" | |
def _mock_response( | |
self, | |
status=200, | |
content="CONTENT", | |
json_data=None, | |
raise_for_status=None): | |
""" | |
since we typically test a bunch of different | |
requests calls for a service, we are going to do | |
a lot of mock responses, so its usually a good idea | |
to have a helper function that builds these things | |
""" | |
mock_resp = mock.Mock() | |
# mock raise_for_status call w/optional error | |
mock_resp.raise_for_status = mock.Mock() | |
if raise_for_status: | |
mock_resp.raise_for_status.side_effect = raise_for_status | |
# set status code and content | |
mock_resp.status_code = status | |
mock_resp.content = content | |
# add json data if provided | |
if json_data: | |
mock_resp.json = mock.Mock( | |
return_value=json_data | |
) | |
return mock_resp | |
@mock.patch('requests.get') | |
def test_google_query(self, mock_get): | |
"""test google query method""" | |
mock_resp = self._mock_response(content="ELEPHANTS") | |
mock_get.return_value = mock_resp | |
result = google_query('elephants') | |
self.assertEqual(result, 'ELEPHANTS') | |
self.assertTrue(mock_resp.raise_for_status.called) | |
@mock.patch('requests.get') | |
def test_failed_query(self, mock_get): | |
"""test case where google is down""" | |
mock_resp = self._mock_response(status=500, raise_for_status=HTTPError("google is down")) | |
mock_get.return_value = mock_resp | |
self.assertRaises(HTTPError, google_query, 'elephants') | |
if __name__ == '__main__': | |
unittest.main() |
How do you mock a response in Python?
The mock_requests. get() should return a mock for the response. To mock the response, you can use the MagicMock class of the unittest. mock module.
How do you mock an object in Python?
How do we mock in Python?.
Write the test as if you were using real external APIs..
In the function under test, determine which API calls need to be mocked out; this should be a small number..
In the test function, patch the API calls..
Set up the MagicMock object responses..
Run your test..
How do you mock response status code?
Go to Rules Tab and Click on New Rule..
Select Modify Response..
Define the exact URL (or Pattern) and define the status code. That's it..
How do you use mock exception in Python?
Just assign the exception to side_effect instead: mockedObj. raiseError. side_effect = Exception("Test") . You don't have to no: and his edit has a third way of doing it where he is making a Mock with the side effect set, but its still a valid, and good thing to know how to do in testing.