respx

A utility for mocking out the Python HTTPX and HTTP Core libraries.

BSD-3-Clause 48 个版本 Python >=3.8
Jonas Lundberg <jonas@5monkeys.se>
安装
pip install respx
poetry add respx
pipenv install respx
conda install respx
描述

RESPX

RESPX - Mock HTTPX with awesome request patterns and response side effects.


tests codecov PyPi Version PyPI Downloads Python Versions

Documentation

Full documentation is available at lundberg.github.io/respx

QuickStart

RESPX is a simple, yet powerful, utility for mocking out the HTTPX, and HTTP Core, libraries.

Start by patching HTTPX, using respx.mock, then add request routes to mock responses.

import httpx
import respx

from httpx import Response


@respx.mock
def test_example():
    my_route = respx.get("https://example.org/").mock(return_value=Response(204))
    response = httpx.get("https://example.org/")
    assert my_route.called
    assert response.status_code == 204

Read the User Guide for a complete walk-through.

pytest + httpx

For a neater pytest experience, RESPX includes a respx_mock fixture for easy HTTPX mocking, along with an optional respx marker to fine-tune the mock settings.

import httpx
import pytest


def test_default(respx_mock):
    respx_mock.get("https://foo.bar/").mock(return_value=httpx.Response(204))
    response = httpx.get("https://foo.bar/")
    assert response.status_code == 204


@pytest.mark.respx(base_url="https://foo.bar")
def test_with_marker(respx_mock):
    respx_mock.get("/baz/").mock(return_value=httpx.Response(204))
    response = httpx.get("https://foo.bar/baz/")
    assert response.status_code == 204

Installation

Install with pip:

$ pip install respx

Requires Python 3.8+ and HTTPX 0.25+. See Changelog for older HTTPX compatibility.

版本列表
0.23.1 2026-04-08
0.23.0 2026-04-07
0.22.0 2024-12-19
0.21.1 2024-03-27
0.21.0 2024-03-19
0.20.2 2023-07-20
0.20.1 2022-11-18
0.20.0 2022-09-16
0.19.3 2022-09-14
0.19.2 2022-02-03
0.19.1 2022-01-10
0.19.0 2021-11-15
0.18.2 2021-10-22
0.18.1 2021-10-20
0.18.0 2021-10-14
0.18.0b0 2021-09-15
0.17.1 2021-07-05
0.17.0 2021-04-27
0.16.3 2020-12-14
0.16.2 2020-11-26
0.16.1 2020-11-16
0.16.0 2020-11-13
0.15.1 2020-11-10
0.15.0 2020-11-09
0.14.0 2020-10-15
0.13.0 2020-09-30
0.12.1 2020-08-21
0.12.0 2020-08-17
0.11.3 2020-08-13
0.11.2 2020-06-25
0.11.1 2020-06-01
0.11.0 2020-05-29
0.10.1 2020-03-11
0.10.0 2020-01-30
0.9.0 2020-01-22
0.8.3 2020-01-10
0.8.2 2020-01-07
0.8.1 2019-12-09
0.8 2019-11-27
0.7.4 2019-11-24
0.7.3 2019-11-21
0.7.2 2019-11-21
0.7.1 2019-11-20
0.7 2019-11-19
0.6 2019-11-18
0.5 2019-11-18
0.4 2019-11-16
0.3.1 2019-11-16