Coder Social home page Coder Social logo

sanic-org / sanic-testing Goto Github PK

View Code? Open in Web Editor NEW
31.0 7.0 18.0 113 KB

Test clients for Sanic

Home Page: https://sanic.dev/en/plugins/sanic-testing/getting-started.html

License: MIT License

Python 99.40% Makefile 0.60%
python sanic testing hacktoberfest

sanic-testing's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.