Testing Utilities
AsyncTestClient
Section titled “AsyncTestClient”For async tests with pytest-asyncio:
import pytestfrom justapi.testing import AsyncTestClientfrom myapp import app
@pytest.fixtureasync def client(): async with AsyncTestClient(app) as c: yield c
@pytest.mark.anyioasync def test_hello(client): response = await client.get("/hello") assert response["status"] == 200ManagedDb
Section titled “ManagedDb”Test database with automatic cleanup:
from justapi.testing import ManagedDb
db = ManagedDb("sqlite:///test.db", migrations_dir="migrations/")
async def setup(): await db.run_migrations()
async def teardown(): await db.run_sql("DELETE FROM users")db_client
Section titled “db_client”Create a test client with a managed database:
from justapi.testing import db_client
async def test_with_db(): async with db_client(app, "sqlite:///test.db") as client: response = await client.get("/users") assert response["status"] == 200Snapshot Testing
Section titled “Snapshot Testing”Assert responses match saved snapshots:
from justapi.testing import Snapshot
snapshot = Snapshot()
def test_api_response(): response = client.get("/items") snapshot.assert_response(response, "items_list") # First run: saves snapshot # Subsequent runs: compares against saved snapshotUpdate snapshots by setting SNAPSHOT_UPDATE=1.
Assertion Helpers
Section titled “Assertion Helpers”from justapi.testing import assert_ok, assert_status, assert_json, assert_header
def test_helpers(): response = client.get("/hello")
assert_ok(response) # status == 200 assert_status(response, 201) # status == 201 assert_json(response, {"message": "Hello"}) # JSON body matches assert_header(response, "X-Custom", "value") # header exists with valueSee Also
Section titled “See Also”- Testing — basic testing with JustAPITestClient
- Testing a Database — database testing patterns