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-httpserverVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Python environment to version 3.10 or newer.
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.
Replace `httpserver.check_assertions()` with `httpserver.check()`. The `check()` method calls both `check_assertions()` and `check_handler_errors()`, providing more comprehensive error checking.
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.
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"})`.Review and update your usage of `expect_request`, `expect_oneshot_request`, and `expect_ordered_request` methods according to the 1.0.0 API documentation.
Install the 'pytest-httpserver' package in your active Python environment: `pip install pytest-httpserver`. Ensure your test runner uses this environment.
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(...)`.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
```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.