Skip to main content

Test Clietn for ASGI web applications

Project description

asgi-testClient

Travis (.org) PyPI - Python Version PyPI codecov PyPI - Downloads black

Testing ASGI applications made easy!

The why?

Why would you build this when all web frameworks come with one? Well, because mostly all those web frameworks have to build their own. I was building my own web framework perhaps (research & learning purpose) and got to the point where a needed a TestClient but then a asked my self why does anybody building web frameworks have to build their own TestClient when there's a standard?. Ok, then just install starlette a use it test client; would you install a library just to use a tiny part of it? This client does not have any dependencies.

Requirements

Python 3.6+

It should run on Python 3.5 but I haven' tested it.

Installation

pip install asgi-testclient

Usage

The client replicates the requests API, so if you have used request you should feel comfortable. Note: the client method are coroutines get, post, delete, put, patch, etc...

import pytest
from asgi_testclient import TestClient

from myapp import API

@pytest.fixture
def client():
    return TestClient(API)

@pytest.mark.asyncio
async def test_get(client):
    response = await client.get("/")
    assert response.json() == {"hello": "world"}
    assert response.status_code == 200

I have used pytest in this example but you can use whichever runner you prefer.

If you still prefer simple functions to coroutines, you can use the sync interface:

import pytest
from asgi_testclient.sync import TestClient

@pytest.fixture
def client():
    return TestClient(API)

def test_get(client):
    response = client.get("/")
    assert response.json() == {"hello": "world"}
    assert response.status_code == 200

Take in account that if you're running inside an async app you should use the async client, yet you can run the sync one inside threads is still desired.

Websockets

If you're using ASGI you may be doing some web-sockets stuff. We have added support for it also, so you can test it easy.

from asgi_testclient import TestClient
from myapp import API

async def test_send():
    echo_server = TestClient(API)
    websocket = await echo_server.ws_connect("/")
    for msg in ["Hey", "Echo", "Back"]:
        await websocket.send_text(msg)
        data = await websocket.receive_text()
        assert data == msg
    await websocket.close()

async def test_ws_context():
    client = TestClient(API)
    async with client.ws_session("/") as websocket:
        data = await websocket.receive_text()
        assert data == "Hello, world!"

Few things to take in count here:

  1. When using ws_connect you must call websocket.close() to finish up your APP task.
  2. For using websockets in context manager you must use ws_session instead of ws_connect.
  3. When waiting on server response websocker.receive_* it may raise a WsDisconnect.

And one more time for those who don't want to this async we got the sync version:p

from asgi_testclient.sync import TestClient
from myapp import API

client = TestClient(API)

def test_send_receive_json():
    websocket = client.ws_connect("/json")

    json_msg = {"hello": "test"}
    websocket.send_json(json_msg)

    assert websocket.receive_json() == json_msg
    websocket.close()

def test_ws_context():
    with client.ws_session("/") as websocket:
        data = websocket.receive_text()
        assert data == "Hello, world!"

Important: In the sync version you cannot use send or receive since they're coroutines, instead use their children send_* or receive_* text|bytes|json.

Also sync version is done throw monkey patching so you can't use both version async & sync at the same time.

TODO:

  • Support Websockets client.
  • Cookies support.
  • Redirects.
  • Support files encoding
  • Stream request & response

Credits

  • Tom Christie: I brought inspiration from the starlette test client.
  • Kenneth ☤ Reitz: This package tries to replicate requests API.

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

asgi-testclient-0.3.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

asgi_testclient-0.3.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file asgi-testclient-0.3.1.tar.gz.

File metadata

  • Download URL: asgi-testclient-0.3.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

File hashes

Hashes for asgi-testclient-0.3.1.tar.gz
Algorithm Hash digest
SHA256 f9fc014604e751978562aaf8fbd72f4d9c140340233f119c0f96894efd9504fc
MD5 1f30eeceff4104df85961bf38e91141c
BLAKE2b-256 9bfb5e1587fa559d8b083e1b271c33205ae8d5c6740a21e394801a64f2d01473

See more details on using hashes here.

File details

Details for the file asgi_testclient-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: asgi_testclient-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

File hashes

Hashes for asgi_testclient-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 279ff81a26330a3b234c55c2fba016e8bad1732f89e217263034872c12873c80
MD5 5b22ad1f4c42aa810431541383e7b0f5
BLAKE2b-256 c78377795a0782ca3f7b9ba586187a5626ebd044ca64120a02d1490f0b2d44c3

See more details on using hashes here.

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