{"id":5428,"library":"pytest-localserver","title":"pytest-localserver","description":"pytest-localserver is a pytest plugin that provides easy-to-use fixtures for testing server connections locally. It allows you to run HTTP(S) and SMTP servers directly within your test suite, simplifying integration tests against external services. The current version is 0.10.0, with releases focusing on Python version support, dependency updates, and minor feature enhancements.","status":"active","version":"0.10.0","language":"en","source_language":"en","source_url":"https://github.com/pytest-dev/pytest-localserver","tags":["pytest","testing","server","http","https","smtp","local","fixture"],"install":[{"cmd":"pip install pytest-localserver","lang":"bash","label":"Base installation"},{"cmd":"pip install 'pytest-localserver[smtp]'","lang":"bash","label":"With SMTP server support"}],"dependencies":[{"reason":"Core dependency for the pytest plugin framework.","package":"pytest","optional":false},{"reason":"Used for the HTTP server implementation.","package":"Werkzeug","optional":false},{"reason":"Used for generating certificates for HTTPS servers.","package":"trustme","optional":false},{"reason":"Provides the asynchronous SMTP server implementation. Only required if using the SMTP server via the '[smtp]' extra.","package":"aiosmtpd","optional":true}],"imports":[{"note":"While 'import' works, 'from ... import http' is common for direct access to its classes like Server.","wrong":"import pytest_localserver.http","symbol":"http","correct":"from pytest_localserver import http"},{"note":"Similar to 'http', 'from ... import smtp' is preferred for direct access to SMTP server components.","wrong":"import pytest_localserver.smtp","symbol":"smtp","correct":"from pytest_localserver import smtp"},{"note":"The primary HTTP server class is named `Server` within the `http` module, not `HTTPServer`.","wrong":"from pytest_localserver.http import HTTPServer","symbol":"Server","correct":"from pytest_localserver.http import Server"}],"quickstart":{"code":"import pytest\nfrom pytest_localserver.http import WSGIHandler, Server\n\n# Define a simple WSGI application\ndef my_app(environ, start_response):\n    status = '200 OK'\n    headers = [('Content-type', 'text/plain; charset=utf-8')]\n    start_response(status, headers)\n    return [b\"Hello, World!\"]\n\n# Create a fixture for the local HTTP server\n@pytest.fixture\ndef http_server():\n    # Instantiate the WSGI handler with your app\n    handler = WSGIHandler(my_app)\n    # Start the server\n    server = Server(handler)\n    server.start()\n    yield server\n    # Stop the server after the test\n    server.stop()\n\n# Example test using the http_server fixture\ndef test_my_app_endpoint(http_server):\n    import requests\n    # Access the server URL provided by the fixture\n    response = requests.get(http_server.url)\n    assert response.status_code == 200\n    assert response.text == \"Hello, World!\"\n","lang":"python","description":"This quickstart demonstrates how to set up a local HTTP server with a simple WSGI application using pytest-localserver. It defines a pytest fixture that starts the server before tests and stops it afterwards. The example then shows a test function that uses `requests` to make a call to the local server's URL and asserts the response."},"warnings":[{"fix":"Upgrade to Python 3.7 or newer, or pin `pytest-localserver<0.10.0`.","message":"Python 3.6 support has been dropped. Users on Python 3.6 must remain on version 0.9.x or earlier.","severity":"breaking","affected_versions":"0.10.0 and later"},{"fix":"Upgrade pytest to version 4.6 or newer, or pin `pytest-localserver<0.9.0.post0`.","message":"The minimum required pytest version was raised to `pytest>=4.6`. Users working with older pytest versions (e.g., `pytest>=4,<4.6`) must stay on `pytest-localserver<0.9.0.post0`.","severity":"breaking","affected_versions":"0.9.0.post0 and later"},{"fix":"Install using `pip install 'pytest-localserver[smtp]'`.","message":"SMTP server support is no longer installed by default. To use the SMTP server, you must install `pytest-localserver` with the `[smtp]` extra.","severity":"breaking","affected_versions":"0.7.0 and later"},{"fix":"Ensure `pytest-localserver` is up-to-date (0.7.1+) if experiencing issues with newer `aiosmtpd` versions or if you need to manually interact with `aiosmtpd`'s API.","message":"The SMTP server internally uses `aiosmtpd`, which had breaking changes (e.g., removal of `_stop` method in 1.4.3+). While `pytest-localserver` attempts to handle this, ensure compatibility if explicitly interacting with `aiosmtpd` versions.","severity":"gotcha","affected_versions":"0.6.0 onwards (due to `aiosmtpd` integration), specifically `aiosmtpd` 1.4.3+ required `pytest-localserver>=0.7.1` for a fix."}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}