Last active
December 18, 2015 02:18
-
-
Save johnboxall/5709599 to your computer and use it in GitHub Desktop.
Not sure if httpbin is down or your tests are failing? Use `mock` to stub out your external calls!
This file contains hidden or 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
import mock | |
import requests | |
import unittest | |
def get(): | |
# Retrieves data from an external service eg. Twitter, Google | |
try: | |
return requests.get('http://external-service.com/') | |
except requests.RequestException: | |
return None | |
class Test(unittest.TestCase): | |
def test(self): | |
# Ensure `get` handles `RequestException`. | |
get_mock = mock.MagicMock() | |
get_mock.side_effect = requests.RequestException() | |
with mock.patch("requests.get", get_mock): | |
self.assertEquals(get(), None) | |
if __name__ == '__main__': | |
unittest.main(verbosity=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment