public
Created

Test an API wrapper using requests.

  • Download Gist
test_api.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.