Skip to main content

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.

Metadata

Release files for asgi-testclient 0.3.1

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

Source distribution (sdist)

Source distribution for asgi-testclient 0.3.1
File Size Uploaded Provenance
asgi-testclient-0.3.1.tar.gz 9.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for asgi-testclient 0.3.1
File Interpreter ABI Platform
asgi_testclient-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.0 kB

Release files / asgi-testclient-0.3.1.tar.gz

Download URL asgi-testclient-0.3.1.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f9fc014604e751978562aaf8fbd72f4d9c140340233f119c0f96894efd9504fc
BLAKE2b-256 checksum
How to use checksums
9bfb5e1587fa559d8b083e1b271c33205ae8d5c6740a21e394801a64f2d01473
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

Release files / asgi_testclient-0.3.1-py3-none-any.whl

Download URL asgi_testclient-0.3.1-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
279ff81a26330a3b234c55c2fba016e8bad1732f89e217263034872c12873c80
BLAKE2b-256 checksum
How to use checksums
c78377795a0782ca3f7b9ba586187a5626ebd044ca64120a02d1490f0b2d44c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

Security for asgi-testclient 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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