swadhinbiswas docs: create 23 new advanced guide and how-to recipe pages · 7/25/2026
54132cc
64 lines · 1.6 KB
Raw Download

title: Async Tests
description: Write async test functions with httpx.AsyncClient for JustAPI applications.
keywords: [JustAPI, async tests, httpx, AsyncClient, pytest-asyncio]

Async Test Client

Use httpx.AsyncClient for async testing:

import pytest
import httpx
from justapi import JustAPIApp

app = JustAPIApp()

@app.get("/hello")
async def hello():
    return {"message": "Hello!"}

@pytest.mark.anyio
async def test_hello():
    async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client:
        response = await client.get("/hello")
        assert response.status_code == 200
        assert response.json() == {"message": "Hello!"}

Testing with Dependencies

def override_get_db():
    return TestDB()

app.dependency_overrides[get_db] = override_get_db

@pytest.mark.anyio
async def test_with_override():
    async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client:
        response = await client.get("/items/")
        assert response.status_code == 200

Setup and Teardown

@pytest.fixture
async def client():
    async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as c:
        yield c

@pytest.mark.anyio
async def test_example(client):
    response = await client.get("/hello")
    assert response.status_code == 200

See Also