Skip to main content

Core testing clients for Sanic

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanic-testing-22.6.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

sanic_testing-22.6.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file sanic-testing-22.6.0.tar.gz.

File metadata

  • Download URL: sanic-testing-22.6.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for sanic-testing-22.6.0.tar.gz
Algorithm Hash digest
SHA256 8f006d2332106539cd6f3da8a5c0d1f31472261f3293e43e2c9bbad605e72c5b
MD5 fb12d417a0e2146409a28faad51e5a4e
BLAKE2b-256 fcfbeca688b7cc4d6250877e63ce0b119309f35dc6de8546e1f5b52ac53b8723

See more details on using hashes here.

Provenance

File details

Details for the file sanic_testing-22.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sanic_testing-22.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d84303e83066de7f18e8c3a0cd04512ba1517dbc31123f14e8aec318b22c008c
MD5 1903c4c9f18b2936af63e9a0f789022d
BLAKE2b-256 50f2df592d4cac9212355a7de5931258f3a25c86ec894a3e50f168956d8d1a00

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page