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.

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

TODO

  • Create convenience decorators to inject TestManager
  • Add more testing from Sanic core to make sure test clients are robust
  • Add ability for reusable servers in sync testing instead of destroying them on every call

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-0.6.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sanic_testing-0.6.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sanic-testing-0.6.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for sanic-testing-0.6.0.tar.gz
Algorithm Hash digest
SHA256 c239b87ed3ea5fe1dc3b5139e6e1173fb4e37bcb05767750522771ca8778e32f
MD5 8731a75ad1515217f16e6088c9185c77
BLAKE2b-256 15131151ed52c8a95e74cb8254bd4c30556ce3e3a8f94d14cfb01817bb4ebeff

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sanic_testing-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for sanic_testing-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc46b48253d0549a94ed18c455dddc766a9a29b7069ce849208ae867517ddf63
MD5 3286014622f9be5b6b5e4743f950f110
BLAKE2b-256 a4abf916241e3a9b1ed3d75b3e7bac9e1fca953cc9c614fc393fb1b10b85aa91

See more details on using hashes here.

Supported by

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