responses

raw JSON →
0.26.0 verified Tue May 12 auth: no python install: verified quickstart: verified

responses is a utility library designed for mocking out the `requests` Python library, making it easier to test code that performs HTTP requests. It allows developers to register mock responses for specific URLs and HTTP methods, controlling the behavior of `requests` during testing. The library is actively maintained, with frequent releases, and is currently at version 0.26.0.

pip install responses
breaking In version 0.26.0, the behavior of `assert_all_requests_are_fired=True` (the default when using `responses.activate` as a context manager or decorator) changed. Assertions about unfired requests are now raised even if an exception occurs within the context manager or decorated function. Previously, these assertions were suppressed, which could hide uncalled mocks.
fix Review tests that might rely on exceptions suppressing `responses` assertions. Adjust tests to explicitly handle the assertion failure, or set `assert_all_requests_are_fired=False` if this validation is not needed.
deprecated Direct access to `assert_all_requests_are_fired`, `passthru_prefixes`, and `target` from the top-level `responses` module (e.g., `responses.assert_all_requests_are_fired`) is deprecated.
fix Use the attributes via `responses.mock`, such as `responses.mock.assert_all_requests_are_fired`, `responses.mock.passthru_prefixes`, and `responses.mock.target` instead.
deprecated The `match_querystring` argument in `Response` and `CallbackResponse` is deprecated.
fix For matching query parameters, use `responses.matchers.query_param_matcher` for dictionary-based matching or `responses.matchers.query_string_matcher` for matching the raw query string.
gotcha By default, `responses.add` assumes a mock should be consumed once. If `assert_all_requests_are_fired=True` (the default) is active, calling the same URL multiple times with a single `responses.add` definition will cause an `AssertionError` after the first call.
fix For URLs expected to be called multiple times, set `repeat=True` in `responses.add()` (e.g., `responses.add(..., repeat=True)`), or add multiple `responses.add()` calls for each expected interaction.
gotcha Prior to version 0.25.0, `matchers.header_matcher` did not correctly fail if a header specified in the matcher was entirely missing from the request; it only validated the value of present headers.
fix Upgrade to `responses` 0.25.0 or later to ensure `matchers.header_matcher` properly validates the presence of matched headers.
gotcha When using `responses` alongside other network mocking libraries (e.g., `httpretty`, `moto`) or attempting to make actual network calls within the same test context, conflicts can arise as `responses` extensively patches the `requests` library.
fix Ensure `responses.activate`'s scope is properly managed and isolated to avoid interference. If possible, avoid combining multiple patching libraries for the same underlying network functionality in a single test, or use `responses.mock.passthru_all` or `responses.add_passthru` to allow specific real requests.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.85s 23.5M
3.10 alpine (musl) - - 0.88s 23.5M
3.10 slim (glibc) wheel 2.2s 0.52s 25M
3.10 slim (glibc) - - 0.58s 25M
3.11 alpine (musl) wheel - 1.22s 25.8M
3.11 alpine (musl) - - 1.30s 25.8M
3.11 slim (glibc) wheel 2.3s 1.00s 27M
3.11 slim (glibc) - - 0.97s 27M
3.12 alpine (musl) wheel - 1.25s 17.6M
3.12 alpine (musl) - - 1.34s 17.6M
3.12 slim (glibc) wheel 2.1s 1.24s 19M
3.12 slim (glibc) - - 1.20s 19M
3.13 alpine (musl) wheel - 1.28s 17.4M
3.13 alpine (musl) - - 1.34s 17.3M
3.13 slim (glibc) wheel 2.1s 1.16s 19M
3.13 slim (glibc) - - 1.26s 18M
3.9 alpine (musl) wheel - 0.74s 22.7M
3.9 alpine (musl) - - 0.80s 22.8M
3.9 slim (glibc) wheel 2.6s 0.69s 24M
3.9 slim (glibc) - - 0.65s 24M

The quickstart demonstrates mocking a GET request using the `@responses.activate` decorator and `responses.add()` method. It sets up a mock response with a JSON body and then makes a `requests.get()` call, which is intercepted by `responses`. Finally, it asserts the response content and verifies that the mock was called.

import requests
import responses

@responses.activate
def test_example_request():
    responses.add(
        responses.GET,
        'http://example.com/api/test',
        json={'message': 'Hello, World!'},
        status=200,
        content_type='application/json'
    )

    # This request will be intercepted by 'responses'
    resp = requests.get('http://example.com/api/test')

    assert resp.status_code == 200
    assert resp.json() == {'message': 'Hello, World!'}

    # Verify that exactly one request was made to the registered URL
    assert len(responses.calls) == 1
    assert responses.calls[0].request.url == 'http://example.com/api/test'

print("Running test_example_request...")
test_example_request()
print("Test completed successfully.")