Requests-mock
The requests-mock library is a requests transport adapter that can be preloaded with responses that are returned if certain URIs are requested. This is particularly useful in unit tests where you want to return known responses from HTTP requests without making actual calls.
Installation⚑
pip install requests-mock
Usage⚑
Object initialization⚑
Select one of the following ways to initialize the mock.
As a pytest fixture⚑
The ease of use with pytest it is awesome. requests-mock
provides an external fixture registered with pytest such that it is usable simply by specifying it as a parameter. There is no need to import requests-mock it simply needs to be installed and specified as an argument in the test definition.
import pytest
import requests
from requests_mock.mocker import Mocker
def test_url(requests_mock: Mocker):
requests_mock.get('http://test.com', text='data')
assert 'data' == requests.get('http://test.com').text
As a function decorator⚑
>>> @requests_mock.Mocker()
... def test_function(m):
... m.get('http://test.com', text='resp')
... return requests.get('http://test.com').text
...
>>> test_function()
'resp'
As a context manager⚑
>>> import requests
>>> import requests_mock
>>> with requests_mock.Mocker() as m:
... m.get('http://test.com', text='resp')
... requests.get('http://test.com').text
...
'resp'
Mocking responses⚑
Return a json⚑
requests_mock.get(
'{}/api/repos/owner/repository/builds'.format(self.url),
json={
"id": 882,
"number": 209,
"finished": 1591197904,
},
)
Add a header or a cookie to the response⚑
requests_mock.post(
"https://test.com",
cookies={"Id": "0"},
headers={"id": "0"},
)
Multiple responses⚑
Multiple responses can be provided to be returned in order by specifying the keyword parameters in a list.
requests_mock.get(
'https://test.com/4',
[
{'text': 'resp1', 'status_code': 300},
{'text': 'resp2', 'status_code': 200}
]
)
Get requests history⚑
Called⚑
The easiest way to test if a request hit the adapter is to simply check the called property or the call_count property.
>>> import requests
>>> import requests_mock
>>> with requests_mock.mock() as m:
... m.get('http://test.com, text='resp')
... resp = requests.get('http://test.com')
...
>>> m.called
True
>>> m.call_count
1
Requests history⚑
The history of objects that passed through the mocker/adapter can also be retrieved.
>>> history = m.request_history
>>> len(history)
1
>>> history[0].method
'GET'
>>> history[0].url
'http://test.com/'