Responses
Utility to mock HTTP responses for the requests library in tests without hitting real endpoints.
Official Documentation ↗responses 0.25+ / requests 2.x / Python 3.9+. Check the official docs for the latest API surface.Installation
pip install responsesMethods
No methods match this filter.
Overview
Responses mocks HTTP calls made through the requests library by matching method and URL and returning configured responses. Tests run deterministically and offline.
Core Methods
Activate interception with a decorator or context manager, register responses with add, and assert call counts. The registry resets between activations.
Usage Notes
Assert the number of calls to catch retry or deduplication bugs, register only the endpoints a test needs, and record real calls to build accurate fixtures.
Common Pitfalls
Unregistered requests raise ConnectionError, which is good for catching unexpected calls but confusing when forgotten. Not activating interception lets tests hit the network. Broad URL matching can hide path bugs.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/getsentry/responses@latest/README.rst · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:10.832Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Read more Exception as Response body_ json A Python object representing the JSON response body. (https://cdn.jsdelivr.net/gh/getsentry/responses@latest/README.rst)
- Read more Matching Requests_ Exception as Response body ––––––––––––– You can pass an Exception as the body to trigger an error on the request: .. (https://cdn.jsdelivr.net/gh/getsentry/responses@latest/README.rst)
- Where boolean represents True or False if the request parameters match and the string is a reason in case of match failure. (https://cdn.jsdelivr.net/gh/getsentry/responses@latest/README.rst)
FAQ
- How is responses different from unittest.mock?
- Responses intercepts at the requests transport layer, so it matches real HTTP semantics including status codes and bodies, which is more realistic than patching a function.
- Can I simulate network errors?
- Yes. Register a response with body=Exception or use the appropriate option to raise a connection error, which lets you test retry paths.
- Does it record real requests?
- Responses can record real interactions with the recorder and generate mock registrations, useful for building fixtures from a live API.
Report a problem with this API docOpens a pre-filled issue on GitHub.