Complete the code to import the correct test client for async FastAPI testing.
from fastapi.testclient import [1]
The TestClient is the standard synchronous test client FastAPI provides. For async tests, you use other tools.
Complete the code to import the async HTTP client from httpx for async FastAPI testing.
from httpx import [1]
AsyncClient from httpx is used for async HTTP requests in tests.
Fix the error in the async test function by completing the missing keyword to await the client call.
async def test_read_main(async_client): response = [1] async_client.get("/") assert response.status_code == 200
Async calls must be awaited to get their result. Here, await is needed before async_client.get.
Fill both blanks to create an async test fixture that yields an AsyncClient for FastAPI testing.
import pytest from httpx import AsyncClient @pytest.fixture async def async_client(): async with AsyncClient(app=[1], base_url=[2]) as client: yield client
The fixture uses the FastAPI app instance and the base URL "http://testserver" for testing.
Fill all three blanks to write an async test that uses the async_client fixture to get the root path and check the JSON response.
async def test_root(async_client): response = await async_client.[1]("/") assert response.status_code == 200 data = response.[2]() assert data == [3]
The test sends a GET request, parses JSON response, and checks it matches the expected dictionary.