Skip to content

Async Tests

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!"}
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
@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