Async Tests
Async Test Client
Section titled “Async Test Client”Use httpx.AsyncClient for async testing:
import pytestimport httpxfrom justapi import JustAPIApp
app = JustAPIApp()
@app.get("/hello")async def hello(): return {"message": "Hello!"}
@pytest.mark.anyioasync 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
Section titled “Testing with Dependencies”def override_get_db(): return TestDB()
app.dependency_overrides[get_db] = override_get_db
@pytest.mark.anyioasync 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 == 200Setup and Teardown
Section titled “Setup and Teardown”@pytest.fixtureasync def client(): async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as c: yield c
@pytest.mark.anyioasync def test_example(client): response = await client.get("/hello") assert response.status_code == 200See Also
Section titled “See Also”- Testing — sync testing with JustAPITestClient
- Testing with Overrides — dependency overrides
- Debugging — debugging techniques