Skip to main content

Sanic Core Test

This package is meant to be the core testing utility and clients for testing Sanic applications. It is mainly derived from sanic.testing which has (or will be) removed from the main Sanic repository in the future.

Documentation

Getting Started

pip install sanic-testing

The package is meant to create an almost seemless transition. Therefore, after loading the package, it will attach itself to your Sanic instance and insert test clients.

from sanic import Sanic
from sanic_testing import TestManager

sanic_app = Sanic(__name__)
TestManager(sanic_app)

This will provide access to both the sync (sanic.test_client) and async (sanic.asgi_client) clients. Both of these clients are also available directly on the TestManager instance.

Writing a sync test

Testing should be pretty much the same as when the test client was inside Sanic core. The difference is just that you need to run TestManager.

import pytest

@pytest.fixture
def app():
    sanic_app = Sanic(__name__)
    TestManager(sanic_app)

    @sanic_app.get("/")
    def basic(request):
        return response.text("foo")

    return sanic_app

def test_basic_test_client(app):
    request, response = app.test_client.get("/")

    assert response.body == b"foo"
    assert response.status == 200

Writing an async test

Testing of an async method is best done with pytest-asyncio installed. Again, the following test should look familiar to anyone that has used asgi_client in the Sanic core package before.

The main benefit of using the asgi_client is that it is able to reach inside your application, and execute your handlers without ever having to stand up a server or make a network call.

import pytest

@pytest.fixture
def app():
    sanic_app = Sanic(__name__)
    TestManager(sanic_app)

    @sanic_app.get("/")
    def basic(request):
        return response.text("foo")

    return sanic_app

@pytest.mark.asyncio
async def test_basic_asgi_client(app):
    request, response = await app.asgi_client.get("/")

    assert response.body == b"foo"
    assert response.status == 200

Release files for sanic-testing 24.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-testing 24.6.0
File Size Uploaded
sanic_testing-24.6.0.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sanic-testing 24.6.0
File Interpreter ABI Platform
sanic_testing-24.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / sanic_testing-24.6.0.tar.gz

Download URL sanic_testing-24.6.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7591ce537e2a651efb6dc01b458e7e4ea5347f6d91438676774c6f505a124731
BLAKE2b-256 checksum
How to use checksums
b2568d31d8a7e0b61633d6358694edfae976e69739b5bd640ceac7989b62e749
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / sanic_testing-24.6.0-py3-none-any.whl

Download URL sanic_testing-24.6.0-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1027184735e88230891aa0461fff84093abfa3bff0f4d29c0f78f42e59efada
BLAKE2b-256 checksum
How to use checksums
0c931d588f1cb9b710b9f22fa78b53d699a8062edc94204d50dd0d78c5f5b495
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page