Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is the "mockator":
- def _mock_requests_method(responses, url, *dummy1, **dummy2):
- """Check the URL and return the expected response."""
- if url not in responses:
- raise AssertionError('URL doesnt exist: {}', url)
- (status, text) = responses[url]
- response = Response()
- response.status_code = status
- response.raw = StringIO.StringIO(text)
- return response
- # This is how you mock Requests in the test:
- @patch('yourmodule.requests.get') # same for posts
- def test_something(self, mocked_get):
- mocked_get.side_effect = functools.partial(_mock_requests_method,
- {'some/url': (200, 'Response for some/url'),
- 'some/other/url': (404, 'Response for some/other/url')}
- )
- # and the rest of your test.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement