Registry / testing / pytest-httpserver

pytest-httpserver

JSON →
library1.1.5pypypi✓ verified 26d ago

pytest-httpserver is a Python package that allows you to start a real HTTP server for your tests. The server can be programmatically configured to respond to requests, providing an easy-to-use API to set up request handlers and shut down gracefully without configuration files or daemons. As the HTTP server runs in a separate thread and listens on a TCP port, it's compatible with any HTTP client. This library facilitates testing HTTP client applications and migrating between client libraries without rewriting tests. It is currently at version 1.1.5 and maintains an active release cadence, with multiple patch releases often occurring monthly or every few months.

pip install pytest-httpserver
INSTALL
IMPORT
SIG · PYTEST-HTTPSERVER
P
pytest-httpserver
testingpythonv1.1.5
Install
1.8s avg
Import
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.000s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

httpserver
def test_my_app(httpserver):
This is the primary pytest fixture provided by the library.
HTTPServer
from pytest_httpserver import HTTPServer
Used for type hinting or when using the library without pytest via its context API.

This quickstart demonstrates how to use the `httpserver` pytest fixture to set up an expected request for `/foobar` that responds with a JSON object. It then uses the `requests` library to make a call to the server's URL and asserts the response. The `url_for()` method constructs the full URL including the dynamically assigned port.

import requests def test_json_client(httpserver): httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"}) response = requests.get(httpserver.url_for("/foobar")) assert response.status_code == 200 assert response.json() == {"foo": "bar"} # To run this, save as `test_example.py` and run `pytest` in the terminal.
Debug
Known issues
breakingPython 3.9 support was officially dropped in version 1.1.4. While code might still function, CI tests are no longer run for this version.
fix
Upgrade your Python environment to version 3.10 or newer.
affects: >=1.1.4
breakingPython 3.8 support was deprecated in version 1.1.2 to enable more robust type hinting. Users on 3.8 should expect potential compatibility issues and no active testing support.
fix
Upgrade your Python environment to version 3.9 or newer. For full support and latest features, upgrade to Python 3.10+ as per the 1.1.4 requirements.
affects: >=1.1.2
deprecatedThe `check_assertions()` method is less preferred starting from version 1.1.4. It is recommended to use the new `check()` method.
fix
Replace `httpserver.check_assertions()` with `httpserver.check()`. The `check()` method calls both `check_assertions()` and `check_handler_errors()`, providing more comprehensive error checking.
affects: >=1.1.4
gotchaPrior to version 1.1.0, there was a known issue where `httpserver` state could leak between function-scoped tests due to improper fixture teardown, leading to inconsistent test results.
fix
Ensure you are using `pytest-httpserver` version 1.1.0 or newer. If upgrading is not possible, explicitly call `httpserver.clear()` at the end of each test to reset the server state.
affects: <1.1.0
gotchaWhen matching query parameters, do not include them directly in the URI path. The underlying Werkzeug library expects query parameters to be specified separately.
fix
Use the `query_string` parameter in `expect_request` or similar methods. For example, `httpserver.expect_request("/foo", query_string="user=bar")` or `httpserver.expect_request("/foo", query_string={"user": "bar"})`.
affects: All versions
breakingVersion 1.0.0 introduced backward-incompatible changes to how request expectations are set. `httpserver.expect_request()` became a general function accepting a `handler_type` parameter, `expect_oneshot_request()` no longer accepts the `ordered` parameter, and `expect_ordered_request()` was introduced as a new method.
fix
Review and update your usage of `expect_request`, `expect_oneshot_request`, and `expect_ordered_request` methods according to the 1.0.0 API documentation.
affects: >=1.0.0
Errors
Common errors & fixes
NameError: name 'httpserver' is not defined
The 'httpserver' pytest fixture is not recognized, usually because the 'pytest-httpserver' package is not installed or available in the test environment.
fix
Install the 'pytest-httpserver' package in your active Python environment: `pip install pytest-httpserver`. Ensure your test runner uses this environment.
AttributeError: 'method' object has no attribute 'respond_with_json'
This error occurs when the 'expect_request' or 'when_requested' method is not called (missing parentheses `()`) before attempting to chain a response method like `respond_with_json` or `respond_with_data`.
fix
Call the request matching method to get a RequestMatcher object, then chain the response method. For example, use `httpserver.expect_request('/path').respond_with_json({'key': 'value'})` instead of `httpserver.expect_request.respond_with_json(...)`.
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Your HTTP client failed to connect to the 'pytest-httpserver' instance, often due to a port conflict, a firewall blocking the connection, or the server not starting correctly.
fix
Ensure no other process is using the default port (8000) or configure 'pytest-httpserver' to use a different port by defining a `pytest_httpserver_port` fixture in your `conftest.py` file:
```python
# conftest.py
import pytest

@pytest.fixture(scope="session")
def pytest_httpserver_port():
    return 8001 # Or any other available port
```
AssertionError: Not all expectations were met
The test configured 'pytest-httpserver' with one or more 'expect_request()' calls, but the HTTP client under test did not make all these expected requests before the test finished.
fix
Verify that your client code makes all the HTTP requests that 'pytest-httpserver' is configured to expect with `expect_request()`. If some requests are optional, use `when_requested()` instead, as it does not enforce that the request must occur.
Upgrade
Version history
1.1.5latest on PyPI · released Feb 14, 2026
Audit
Dependencies
pytestrequiredIt's a pytest plugin and provides the test framework context.
werkzeugrequiredUnderlying HTTP server library used for handling requests and responses.
Agent activity
7 hits · last 30 days
node
6
Resources
pytest-httpserver — pip install pytest-httpserver · libregistry