Registry / testing / sanic-testing

sanic-testing

JSON →
library24.6.0pypypiunverified

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.

pip install sanic-testing
INSTALL
IMPORT
SIG · SANIC-TESTING
S
sanic-testing
testingpythonv24.6.0
Install
2.1s avg
Import
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v24.6.0 · 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.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 22MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.1s · import 0.000s · 23MB
20MB installed
● package 20MB
Code
Verified usage

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

TestManager
from sanic_testing import TestManager
from sanic_testing import TestManager

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.

import pytest from sanic import Sanic, response from sanic_testing import TestManager @pytest.fixture def app(): sanic_app = Sanic("TestSanic") TestManager(sanic_app) # Attaches test clients to the app instance @sanic_app.get("/") async def basic(request): return response.text("foo") return sanic_app def test_basic_sync_client(app): # Use the sync test client available via app.test_client request, response = app.test_client.get("/") assert request.method.lower() == "get" assert response.body == b"foo" assert response.status == 200 @pytest.mark.asyncio async def test_basic_asgi_client(app): # Use the async ASGI client available via app.asgi_client request, response = await app.asgi_client.get("/") assert request.method.lower() == "get" assert response.body == b"foo" assert response.status == 200
Debug
Known issues
breakingThe 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`.
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`.
affects: Sanic < 21.3, sanic-testing < 21.3
breakingPython 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.
fix
Upgrade your Python environment to 3.8 or newer. Consider using Python 3.10+ for optimal support.
affects: sanic-testing >= 23.6.0
gotchaMonkeypatching 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.
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.
affects: All versions
gotchaWhen testing multiple Sanic application instances, especially in parallel test environments, you might encounter `SanicException: Sanic app name 'xxxxxx' already in use`.
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.
affects: Sanic versions 20.12.0+
gotchaThe 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.
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`.
affects: sanic-testing < 23.3.0
Upgrade
Version history
24.6.0latest on PyPI · released Jun 30, 2024
Audit
Dependencies
sanicrequiredCore web framework being tested; requires Sanic 21.3+.
httpxrequiredUsed internally by SanicTestClient for making HTTP requests.
pytest-asynciooptionalRecommended for writing asynchronous tests with pytest.
Agent activity
19 hits · last 30 days
node
18
Resources