new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

FastAPI · all subjects

testing

12 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

TestClient dependency

The httpx library is required if you want to use the TestClient for testing FastAPI applications.

AsyncClient for async tests instead of TestClient

For async tests, use AsyncClient from HTTPX directly instead of TestClient. Create an AsyncClient with the app and send async requests to it using await syntax.

Async test function signature with async def

When using @pytest.mark.anyio, the test function must be declared with async def, not def. This is required to allow the use of await inside the test.

Calling other async functions in async tests

In async test functions, you can call and await other async functions in addition to sending requests to your FastAPI application, just as you would in other parts of your code.

TestClient based on HTTPX library

TestClient is built on top of HTTPX. HTTPX can be used directly to test the API, specifically using AsyncClient for async tests.

AsyncClient for async tests instead of TestClient

When writing asynchronous tests using async def, you cannot use TestClient inside async functions. Instead, use AsyncClient from HTTPX to send async requests to the FastAPI application. AsyncClient works with await syntax for making asynchronous requests.

pytest.mark.anyio decorator for async test functions

Use @pytest.mark.anyio decorator on test functions to tell pytest that the test should be called asynchronously. This marker enables running test functions as async def instead of regular def.

TestClient magic does not work in async test functions

The TestClient uses magic to call the asynchronous FastAPI application from synchronous test functions, but this magic does not work when the test function itself is asynchronous. When using async test functions, you must use AsyncClient directly instead.

AsyncClient syntax for making requests in tests

To make an asynchronous request with AsyncClient, pass the app to AsyncClient(), then use await with async request methods. Example: async with AsyncClient(app=app, base_url="http://test") as client: response = await client.get('/')

Async tests can call other async functions

When test functions are asynchronous, you can call and await other async functions in your tests, such as async database queries or other async operations, just as you would in regular code.

AsyncClient does not trigger lifespan events

The AsyncClient does not automatically trigger lifespan events (startup and shutdown). To ensure lifespan events are triggered in async tests, use LifespanManager from the florimondmanca/asgi-lifespan library.

RuntimeError with event loop in async tests

If you encounter a RuntimeError: Task attached to a different loop when integrating asynchronous function calls in tests (for example with MongoDB's MotorClient), instantiate objects that need an event loop only within async functions, such as in @app.on_event("startup") callbacks.

Give your agent this brain