Skip to content
Switch branches/tags
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time
Mar 5, 2020
Mar 5, 2020
Mar 5, 2020
Mar 5, 2020
Oct 10, 2020
Mar 5, 2020
Mar 5, 2020
Mar 5, 2020
Mar 5, 2020
Jul 26, 2020
Mar 5, 2020
Mar 5, 2020



release lic ci coverage


Simplified requests calls mocking for pytest

Provides response_mock fixture, exposing simple context manager.

Any request under that manager will be intercepted and mocked according to one or more rules passed to the manager. If actual request won't fall under any of given rules then an exception is raised (by default).

Rules are simple strings, of the pattern: HTTP_METHOD URL -> STATUS_CODE :BODY.


  • Python 3.6+


When this package is installed response_mock is available for pytest test functions.

def for_test():
    return requests.get('http://some.domain')

def test_me(response_mock):

    # Pass response rule as a string,
    # or many rules (to mock consequent requests) as a list of strings/bytes.
    # Use optional `bypass` argument to disable mock conditionally.

    with response_mock('GET http://some.domain -> 200 :Nice', bypass=False):

        result = for_test()

        assert result.ok
        assert result.content == b'Nice'

Describe response header fields using multiline strings:

with response_mock(
    GET http://some.domain

    Allow: GET, HEAD
    Content-Language: ru

    -> 200 :OK

Test json response:

response = json.dumps({'key': 'value', 'another': 'yes'})

with response_mock(f'POST http://some.domain -> 400 :{response}'):

To test binary response pass rule as bytes:

with response_mock(b'GET http://some.domain -> 200 :' + my_bytes):

Access underlying RequestsMock (from responses package) as mock:

with response_mock('HEAD http://some.domain -> 200 :Nope') as mock: