{"id":2724,"library":"pytest-httpserver","title":"pytest-httpserver","description":"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.","status":"active","version":"1.1.5","language":"en","source_language":"en","source_url":"https://github.com/csernazs/pytest-httpserver","tags":["pytest","testing","http","mocking","server","web development"],"install":[{"cmd":"pip install pytest-httpserver","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"It's a pytest plugin and provides the test framework context.","package":"pytest","optional":false},{"reason":"Underlying HTTP server library used for handling requests and responses.","package":"werkzeug","optional":false}],"imports":[{"note":"This is the primary pytest fixture provided by the library.","symbol":"httpserver","correct":"def test_my_app(httpserver):"},{"note":"Used for type hinting or when using the library without pytest via its context API.","symbol":"HTTPServer","correct":"from pytest_httpserver import HTTPServer"}],"quickstart":{"code":"import requests\n\ndef test_json_client(httpserver):\n    httpserver.expect_request(\"/foobar\").respond_with_json({\"foo\": \"bar\"})\n    \n    response = requests.get(httpserver.url_for(\"/foobar\"))\n    assert response.status_code == 200\n    assert response.json() == {\"foo\": \"bar\"}\n\n# To run this, save as `test_example.py` and run `pytest` in the terminal.","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to version 3.10 or newer.","message":"Python 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.","severity":"breaking","affected_versions":">=1.1.4"},{"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.","message":"Python 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.","severity":"breaking","affected_versions":">=1.1.2"},{"fix":"Replace `httpserver.check_assertions()` with `httpserver.check()`. The `check()` method calls both `check_assertions()` and `check_handler_errors()`, providing more comprehensive error checking.","message":"The `check_assertions()` method is less preferred starting from version 1.1.4. It is recommended to use the new `check()` method.","severity":"deprecated","affected_versions":">=1.1.4"},{"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.","message":"Prior 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.","severity":"gotcha","affected_versions":"<1.1.0"},{"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\"})`.","message":"When matching query parameters, do not include them directly in the URI path. The underlying Werkzeug library expects query parameters to be specified separately.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}