Skip to main content

pytest fixture for HTTPX

Project description

pytest fixture for HTTPX

pypi version Build status Coverage Code style: black Number of tests Number of downloads

Notice: This module is still under development, versions prior to 1.0.0 are subject to breaking changes without notice.

Use pytest_httpx.httpx_mock pytest fixture to mock httpx requests.

Add responses

import httpx
from pytest_httpx import httpx_mock, HTTPXMock


def test_something(httpx_mock: HTTPXMock):
    httpx_mock.add_response("http://test_url")

    response = httpx.get("http://test_url")

In case more than one request is sent to the same URL, the responses will be sent in the registration order.

First response will be sent as response of the first request and so on.

If the number of responses is lower than the number of requests on an URL, the last response will be used to reply to all subsequent requests on this URL.

If all responses are not sent back during test execution, the test case will fail at teardown.

Check sent requests

import httpx
from pytest_httpx import httpx_mock, HTTPXMock


def test_something(httpx_mock: HTTPXMock):
    httpx_mock.add_response("http://test_url")

    response = httpx.get("http://test_url")

    request = httpx_mock.get_request("http://test_url")

A request can only be retrieved once per test case.

Calling order is preserved, so in case more than one request is sent to the same URL, the first one will be returned first.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_httpx-0.0.2.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

pytest_httpx-0.0.2-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page