Async ASGI TestClient

1.4.11 · active · verified Thu Apr 16

Async ASGI TestClient is a framework-agnostic library for testing web applications that implement the ASGI specification (versions 2 and 3). It allows direct interaction with an ASGI application within the same asyncio loop as the tests, eliminating the need for a separate HTTP server. Inspired by Quart's testing module, it supports features like cookies, multipart/form-data, redirects, and streaming for both requests and responses, as well as websocket testing. The current version is 1.4.11.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to test a simple Quart (ASGI) application using `async-asgi-testclient` with `pytest` and `pytest-asyncio`. The `TestClient` is used as an async context manager to send requests and assert responses.

import pytest
from quart import Quart, jsonify
from async_asgi_testclient import TestClient

# A dummy ASGI app for testing
app = Quart(__name__)

@app.route('/')
async def root():
    return 'plain response'

@app.route('/json')
async def json_endpoint():
    return jsonify({"hello": "world"})

@pytest.mark.asyncio
async def test_quart_app():
    async with TestClient(app) as client:
        # Test GET request to root
        resp = await client.get("/")
        assert resp.status_code == 200
        assert resp.text == "plain response"

        # Test GET request to JSON endpoint
        resp = await client.get("/json")
        assert resp.status_code == 200
        assert resp.json() == {"hello": "world"}

view raw JSON →