Skip to content

Instantly share code, notes, and snippets.

@RobSpectre
Created August 1, 2013 15:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobSpectre/6132631 to your computer and use it in GitHub Desktop.
Save RobSpectre/6132631 to your computer and use it in GitHub Desktop.
Test an API wrapper using requests.
import unittest
from mock import patch
import mymodule
class APITest(unittest.TestCase):
@patch('requests.get')
def test_wrapped_example(self, MockRequests):
mock_request = MockRequests()
# Set your status code you want to test against
mock_request.status_code = 200
# Set the response content you want to test against
mock_request.text = "{'Your expected response': 'Stuff'}"
# Make your request
response = mymodule.method()
# Test the result.
self.assertTrue(response.status_code == 200)
@akashynwa
Copy link

Can you share your mymodule.py file as well to get a clearer understanding of whats going on here.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment