Skip to content

Instantly share code, notes, and snippets.

@elena-roff
Created March 12, 2019 13:12
Show Gist options
  • Select an option

  • Save elena-roff/57f90e342aa47e6f89a12852facfc5e7 to your computer and use it in GitHub Desktop.

Select an option

Save elena-roff/57f90e342aa47e6f89a12852facfc5e7 to your computer and use it in GitHub Desktop.
Test examples to test API
import requests
from custom_module import api_get_data
uri = 'http://<.....>'
def fail(*args, **kwargs):
raise requests.exceptions.RequestException()
# <.....> to be replaced
def test_api_get_empty_data(requests_mock, mocker):
# in case environments variable was used
mocker.patch.dict('os.environ', {API_URI': 'http://<...>'})
# empty response
requests_mock.register_uri('GET', '{}'.format(uri), json=[])
assert api_get_data() == []
def test_api_get_valid_data(requests_mock, mocker):
requests_mock.register_uri('GET', '{}'.format(uri), json=[{<valid response>}])
response = api_get_data()
assert response['<key>'] == '<value of a key'
def test_fail(mocker):
path = '<path to api call>.requests.get'
with mocker.patch(path, side_effect=fail):
assert api_get_data() == []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment