Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. # This is the "mockator":
  2. def _mock_requests_method(responses, url, *dummy1, **dummy2):
  3. """Check the URL and return the expected response."""
  4. if url not in responses:
  5. raise AssertionError('URL doesnt exist: {}', url)
  6. (status, text) = responses[url]
  7. response = Response()
  8. response.status_code = status
  9. response.raw = StringIO.StringIO(text)
  10. return response
  11.  
  12.  
  13. # This is how you mock Requests in the test:
  14. @patch('yourmodule.requests.get') # same for posts
  15. def test_something(self, mocked_get):
  16. mocked_get.side_effect = functools.partial(_mock_requests_method,
  17. {'some/url': (200, 'Response for some/url'),
  18. 'some/other/url': (404, 'Response for some/other/url')}
  19. )
  20. # and the rest of your test.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement