Skip to content

Instantly share code, notes, and snippets.

@IanMulvany
Last active October 17, 2016 08:31
Show Gist options
  • Save IanMulvany/d0f97e6291828ee2a3b071ac672285f0 to your computer and use it in GitHub Desktop.
Save IanMulvany/d0f97e6291828ee2a3b071ac672285f0 to your computer and use it in GitHub Desktop.
import requests as r
import responses # type: ignore
from requests import Response
"""
This sample code provides two test functions, one using responses, the other does not.
If you run this code whilst online, both test functions will pass, but if you attempt
to run this script whilst offline, only the test function using responses will succeed,
demonstrating that we have managed to mock the API call successfully.
We are also annotating the functions to allow for static type checking, but this is optional, and
provided here as the static type checking tool is reaching maturtuy just as I was putting this
sample code together.
To install the dependcies:
$ pip3 install requests
$ pip3 install responses
and if you want to play with static type checking, you can optionally install
$ pip3 install mypy-lang
To run this sample code:
$ python3 test_respones_example.py
To do static type checking on this code we can use the `mypy` tool:
$ mypy test_respones_example.py
For more information on static type checking have a look at:
http://blog.zulip.org/2016/10/13/static-types-in-python-oh-mypy/ for more details.
The `type: ignore` annotation in the import statemetns above instructs mypy not to concern itself with tyring to infer types for the responses library. This supresses an error that we would otherwise getwhen we run `mypy test_responses_examples.py`
"""
# as of 2016-10-15 jsonplaceholder.typicode.com provides a test HTTP endpoint
test_url = "https://jsonplaceholder.typicode.com/posts/1"
# We mock the response from the above API by inserting the expected JSON response manually
# into the responses object.
# We know ahead of time that the expected response body form this api will be:
response_body = """{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}"""
def get_api_call(url):
# type: (str) -> Response
response = r.get(url)
return response
# activate the the responses api using a decorator
@responses.activate
def test_with_responses_get_api_call():
body_json_response = response_body
# setup the respone for the given named url, in this case `test_url`
responses.add(responses.GET, test_url,
body=body_json_response,
content_type="application/json")
# now we try out our generic api calling function
# we expect that the requests library will now be mocked by the respones library
response = get_api_call(test_url)
assert response.status_code == 200
def test_without_responses_get_api_call():
# here we call our generic api calling function, and we expect that the requests library will not be mocked.
response = get_api_call(test_url)
assert response.status_code == 200
print("testing with responses")
test_with_responses_get_api_call()
print("testing without responses")
test_without_responses_get_api_call()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment