DEV.co
Open-Source Testing · lundberg

respx

RESPX is a Python library that mocks HTTPX HTTP client requests for testing purposes. It lets developers define request patterns and mock responses without making real network calls, integrating seamlessly with pytest.

Source: GitHub — github.com/lundberg/respx
814
GitHub stars
61
Forks
Python
Primary language
BSD-3-Clause
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorylundberg/respx
Ownerlundberg
Primary languagePython
LicenseBSD-3-Clause — OSI-approved
Stars814
Forks61
Open issues24
Latest release0.23.1 (2026-04-08)
Last updated2026-06-19
Sourcehttps://github.com/lundberg/respx

What respx is

RESPX patches HTTPX and HTTP Core libraries to intercept requests, providing a routing system for matching request patterns and returning mock responses. It offers both decorator and pytest fixture approaches, supporting configuration via pytest markers.

Quickstart

Get the respx source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/lundberg/respx.gitcd respx# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Unit testing HTTP-dependent services

Mock external API calls in service layers without network overhead or flaky dependencies. Define deterministic responses for various request patterns.

Integration testing with controlled responses

Test error handling, edge cases, and side effects by mocking specific HTTP endpoints with predictable payloads and status codes.

pytest-based CI/CD test suites

Leverage pytest fixtures and markers for clean, maintainable test code with reusable mock configurations across test modules.

Implementation considerations

  • Confirm HTTPX version compatibility: requires HTTPX 0.25+ and Python 3.8+. Check changelog for older version support.
  • Choose between decorator (@respx.mock) and pytest fixture (respx_mock) based on test structure; fixtures are generally preferred for pytest suites.
  • Define routes with sufficient specificity to avoid unintended matches; overly broad patterns can mask bugs in request construction.
  • For async test coroutines, ensure mock context is properly entered and exited; pytest-asyncio integration may require explicit async fixture scope.
  • Monitor response side effects (headers, cookies, redirects) if your code relies on them; mock responses may need explicit configuration.

When to avoid it — and what to weigh

  • Testing actual network behavior — RESPX mocks requests entirely; it cannot validate real SSL/TLS handshakes, connection pooling, or timeout behavior of actual HTTP stacks.
  • Non-HTTPX HTTP clients — RESPX only patches HTTPX and HTTP Core. If your codebase uses requests, urllib3, or other HTTP libraries, you'll need alternative mocking strategies.
  • Production monitoring or traffic capture — RESPX is a testing utility only. It is not suitable for recording real traffic, profiling live endpoints, or production request interception.
  • Complex async/sync hybrid codebases without careful setup — While RESPX supports both async and sync, incorrect mock scope or fixture configuration can lead to race conditions or leaked mocks across tests.

License & commercial use

BSD-3-Clause license permits commercial use, modification, and distribution provided original copyright and license text are included and the name is not used for endorsement.

BSD-3-Clause is a permissive OSI-approved license. Commercial use is permitted under its terms. No proprietary restrictions, but include license text in distributions and respect the liability disclaimers.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitStrong
Assessment confidenceHigh
Security considerations

RESPX is a testing mock library and does not handle real credentials or network security. Ensure mock responses do not accidentally log sensitive data in test output. No inherent vulnerabilities disclosed; review CHANGELOG for any past security patches. Mocking also prevents validation of real SSL/TLS certificate chains during testing—ensure separate security validation in integration or staging environments.

Alternatives to consider

pytest-httpserver

Spins up a real local HTTP server for testing. Better for validating actual HTTP server behavior but slower and more complex than mocking.

responses (or responses-mock)

Mocks the requests library instead of HTTPX. Choose if your codebase uses requests rather than httpx.

unittest.mock + monkeypatch

Bare-bones manual patching without respx's routing DSL. Lower-level control but more verbose and error-prone for HTTP-heavy test suites.

Software development agency

Build on respx with DEV.co software developers

Our engineers can guide you through mocking strategies, test suite design, and integration with your CI/CD pipeline. Let's discuss your testing needs.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

respx FAQ

Does RESPX work with async httpx code?
Yes. RESPX supports both sync and async HTTPX clients. Use async/await syntax in your tests and ensure the respx_mock fixture is properly scoped for async test functions (e.g., @pytest.mark.asyncio).
Can I mock websocket or streaming responses?
RESPX is designed for standard HTTP request/response mocking. WebSocket and server-sent event (SSE) mocking is not clearly documented; requires review of the docs or source for edge-case support.
How do I ensure mocks don't leak between tests?
Use the respx_mock pytest fixture (function scope by default) rather than manual @respx.mock decorators. Fixture scope is automatically reset after each test.
What if I need to mock multiple independent mock instances?
RESPX supports nested mock contexts and multiple respx_mock fixtures with explicit parameters. See the User Guide for advanced fixture configuration and respx marker options.

Software development & web development with DEV.co

DEV.co helps companies turn open-source tools like respx into production software. Our software development services cover the full lifecycle — architecture, web development, integration, and maintenance — delivered by software developers and web developers who ship. Engage our software development agency to implement or customize it for your open-source testing stack.

Need Help Implementing RESPX?

Our engineers can guide you through mocking strategies, test suite design, and integration with your CI/CD pipeline. Let's discuss your testing needs.