{"id":9291,"library":"sanic-testing","title":"Sanic Testing","description":"Sanic Testing provides core testing utilities and clients for Sanic applications. This package externalizes the testing functionality previously integrated directly into the main Sanic framework. It is actively maintained, with releases typically aligning with the Sanic release cycle. The current version is 24.6.0.","status":"active","version":"24.6.0","language":"en","source_language":"en","source_url":"https://github.com/sanic-org/sanic-testing/","tags":["testing","sanic","web framework","async","pytest"],"install":[{"cmd":"pip install sanic-testing","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core web framework being tested; requires Sanic 21.3+.","package":"sanic","optional":false},{"reason":"Used internally by SanicTestClient for making HTTP requests.","package":"httpx","optional":false},{"reason":"Recommended for writing asynchronous tests with pytest.","package":"pytest-asyncio","optional":true}],"imports":[{"note":"The testing client was moved from Sanic core to the `sanic-testing` library in Sanic v21.3. Instantiate `TestManager` with your Sanic app to access `app.test_client` and `app.asgi_client`.","wrong":"from sanic.testing import SanicTestClient","symbol":"TestManager","correct":"from sanic_testing import TestManager"},{"note":"While `TestManager` is the primary way to integrate, `SanicTestClient` can be imported directly if needed.","symbol":"SanicTestClient","correct":"from sanic_testing.testing import SanicTestClient"}],"quickstart":{"code":"import pytest\nfrom sanic import Sanic, response\nfrom sanic_testing import TestManager\n\n@pytest.fixture\ndef app():\n    sanic_app = Sanic(\"TestSanic\")\n    TestManager(sanic_app) # Attaches test clients to the app instance\n\n    @sanic_app.get(\"/\")\n    async def basic(request):\n        return response.text(\"foo\")\n\n    return sanic_app\n\ndef test_basic_sync_client(app):\n    # Use the sync test client available via app.test_client\n    request, response = app.test_client.get(\"/\")\n    assert request.method.lower() == \"get\"\n    assert response.body == b\"foo\"\n    assert response.status == 200\n\n@pytest.mark.asyncio\nasync def test_basic_asgi_client(app):\n    # Use the async ASGI client available via app.asgi_client\n    request, response = await app.asgi_client.get(\"/\")\n    assert request.method.lower() == \"get\"\n    assert response.body == b\"foo\"\n    assert response.status == 200","lang":"python","description":"This quickstart demonstrates how to set up a Sanic application for testing with `sanic-testing` using `pytest`. It shows both the synchronous `app.test_client` and the asynchronous `app.asgi_client` for making requests against your Sanic application. Remember to install `pytest-asyncio` for async tests."},"warnings":[{"fix":"Install `sanic-testing` via `pip install sanic-testing` and update your import statements from `from sanic.testing import ...` to `from sanic_testing import TestManager`.","message":"The Sanic testing client (originally `sanic.testing`) was moved out of the main Sanic repository into the standalone `sanic-testing` package. Sanic versions prior to 21.3 had it built-in. Users upgrading Sanic to 21.3+ must explicitly install `sanic-testing`.","severity":"breaking","affected_versions":"Sanic < 21.3, sanic-testing < 21.3"},{"fix":"Upgrade your Python environment to 3.8 or newer. Consider using Python 3.10+ for optimal support.","message":"Python 3.7 support has been officially dropped. `sanic-testing` versions 23.6.0 and higher require Python 3.8 or newer, aligning with Sanic's own support policy. Python 3.7 reached end-of-life on June 27, 2023.","severity":"breaking","affected_versions":"sanic-testing >= 23.6.0"},{"fix":"When mocking `httpx` for outgoing requests in your Sanic application, ensure your mock selectively bypasses or handles calls made to the internal test server (e.g., `127.0.0.1`) to allow `sanic-testing`'s client to function correctly. A common pattern is to inspect the URL in your mock.","message":"Monkeypatching the `httpx` library directly can inadvertently affect `sanic-testing`'s internal HTTP client calls, leading to unexpected test behavior or infinite recursion. This happens because `sanic-testing` uses `httpx` under the hood.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Initialize your Sanic apps with unique names (e.g., `Sanic(str(uuid.uuid4()))`). In `sanic-testing` (Sanic 21.3+), `TestManager` helps manage this. For older Sanic versions, setting `Sanic.test_mode = True` or the environment variable `SANIC_REGISTER=False` can also resolve the issue.","message":"When testing multiple Sanic application instances, especially in parallel test environments, you might encounter `SanicException: Sanic app name 'xxxxxx' already in use`.","severity":"gotcha","affected_versions":"Sanic versions 20.12.0+"},{"fix":"Review any tests that directly inspect the ASGI scope (`request.scope`) when using the `asgi_client` and adjust for the presence of `raw_path`.","message":"The ASGI scope for the test client was updated to include `raw_path`. Tests relying on the exact structure or absence of certain keys in the ASGI scope might need adjustments.","severity":"gotcha","affected_versions":"sanic-testing < 23.3.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"When creating your Sanic app instances for testing, ensure each has a unique name, e.g., `sanic_app = Sanic(str(uuid.uuid4()))`. The `TestManager` (from `sanic-testing`) generally handles test mode activation, but for older Sanic versions or specific setups, setting `Sanic.test_mode = True` or the environment variable `SANIC_REGISTER=False` might be necessary before app instantiation.","cause":"Attempting to instantiate multiple Sanic applications with the same name, or Sanic registering an app when running tests in a way that causes conflicts.","error":"SanicException: Sanic app name 'my_app' already in use"},{"fix":"Ensure `httpx` is updated to a compatible version with your `sanic-testing` and `sanic` versions. Check the `sanic-testing` changelog and your `sanic` version's requirements for specific `httpx` compatibility. For example, `sanic-testing` v23.6.0 allowed `httpx 0.24`.","cause":"This error can occur if there's a mismatch or conflict in the `httpx` version required by `sanic-testing` and another dependency, or if the `httpx` client's methods are being called incorrectly.","error":"TypeError: 'URL' object is not callable"}]}