{"id":1697,"library":"respx","title":"respx - HTTPX Mocking Utility","description":"respx is a powerful and flexible utility for mocking out the Python HTTPX and HTTP Core libraries, essential for writing fast and reliable tests for applications that make HTTP requests. It allows developers to intercept HTTP requests and provide predefined responses. The library is actively maintained with frequent releases, currently at version 0.23.1, often updating to align with new HTTPX versions.","status":"active","version":"0.23.1","language":"en","source_language":"en","source_url":"https://github.com/lundberg/respx","tags":["testing","mocking","http","httpx","web_testing","unit_testing"],"install":[{"cmd":"pip install respx","lang":"bash","label":"Install respx"}],"dependencies":[{"reason":"respx is designed to mock httpx; httpx is a required runtime dependency (>=0.23.0,<1.0.0).","package":"httpx","optional":false}],"imports":[{"note":"The main library module.","wrong":null,"symbol":"respx","correct":"import respx"},{"note":"Used for creating mock responses, typically imported directly from httpx.","wrong":null,"symbol":"Response","correct":"from httpx import Response"},{"note":"Common decorator for mocking HTTP requests within a test function.","wrong":null,"symbol":"mock (decorator)","correct":"@respx.mock"},{"note":"Wildcard pattern matcher for request details.","wrong":null,"symbol":"ANY","correct":"from respx import ANY"}],"quickstart":{"code":"import respx\nimport httpx\nfrom httpx import Response\n\n@respx.mock\ndef test_mocked_api_call():\n    # Define a mock response for a GET request to a specific URL\n    respx.get(\"https://api.example.com/users/1\")\\\n        .mock(return_value=Response(200, json={\n            \"id\": 1,\n            \"name\": \"Jane Doe\",\n            \"email\": \"jane.doe@example.com\"\n        }))\n\n    # Make the actual HTTPX request, which will be intercepted by respx\n    response = httpx.get(\"https://api.example.com/users/1\")\n\n    # Assertions to verify the mocked response\n    assert response.status_code == 200\n    assert response.json()[\"name\"] == \"Jane Doe\"\n    print(f\"Mocked response received: {response.json()}\")\n\n    # Verify that the mock was called\n    assert respx.calls.call_count == 1\n    assert respx.calls.called\n\nif __name__ == '__main__':\n    print(\"Running mocked API call example...\")\n    test_mocked_api_call()\n    print(\"Example finished successfully.\")\n","lang":"python","description":"This quickstart demonstrates how to use the `@respx.mock` decorator to intercept an HTTPX GET request. It defines a mock response for a specific URL, then performs an HTTPX request that transparently receives the mocked data. Finally, it asserts the response and verifies that the mock was called."},"warnings":[{"fix":"Upgrade to Python 3.8+ or pin `respx` version to `<0.22.0`.","message":"respx dropped support for Python 3.7 in version 0.22.0 to align with HTTPX 0.25.0. Projects using Python 3.7 must pin respx to <0.22.0.","severity":"breaking","affected_versions":">=0.22.0"},{"fix":"Update code to catch `AttributeError` or ensure a call/response exists before accessing these properties. For example, check `if respx.calls.called:` before `respx.calls.last.response`.","message":"Since version 0.20.0, `Call.response` and `CallList.last` now raise an `AttributeError` (or similar for missing calls) instead of returning `None` if a response or call is not found. This changes the expected behavior for type-hinted code or code implicitly handling `None`.","severity":"breaking","affected_versions":">=0.20.0"},{"fix":"Upgrade to `respx` version 0.23.1 or later to ensure correct `params` pattern matching.","message":"A regression in version 0.23.0 caused `params` pattern matching to stop working under some conditions, especially with `ANY` in multi-item patterns. This was fixed in 0.23.1.","severity":"gotcha","affected_versions":"0.23.0"},{"fix":"Upgrade to `respx` version 0.23.0 or later to ensure robust `ANY` pattern matching in multi-item contexts.","message":"Prior to version 0.23.0, the `ANY` wildcard in `MultiItems` patterns (used for headers, params, or data with multiple values) might not have been handled properly, potentially leading to unexpected mismatches.","severity":"gotcha","affected_versions":"<0.23.0"},{"fix":"Upgrade to `respx` version 0.21.0 or later to use the `files` pattern for easier matching of file uploads.","message":"Before version 0.21.0, direct matching on `files` (multipart/form-data uploads) was not officially supported and might have required custom workarounds. The `files` pattern was added in 0.21.0.","severity":"gotcha","affected_versions":"<0.21.0"},{"fix":"Upgrade to `respx` version 0.19.3 or later for improved compatibility and stability when using the decorator with `pytest` fixtures.","message":"Combining the `@respx.mock` decorator with `pytest` fixtures prior to version 0.19.3 could sometimes lead to unexpected behavior or conflicts, such as the mock not being active or pytest's function wrapping being incorrect.","severity":"gotcha","affected_versions":"<0.19.3"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}