new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Advanced · all subjects

testing

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

AsyncClient replaces TestClient in async tests

When writing asynchronous test functions, you cannot use the TestClient inside async functions because TestClient uses magic that does not work within asynchronous contexts. Instead, use AsyncClient from HTTPX directly to make asynchronous requests to your FastAPI application using await.

pytest.mark.anyio decorator for async tests

Use the @pytest.mark.anyio decorator on test functions to tell pytest that the test function should be called asynchronously. The test function must be defined with async def instead of def when using this decorator.

AsyncClient syntax for making requests

Create an AsyncClient with your FastAPI app and send requests using await syntax. For example: async with AsyncClient(app=app, base_url='http://test') as client: response = await client.get('/'). This is the async equivalent to synchronous TestClient requests.

AsyncClient does not trigger lifespan events

AsyncClient will not trigger lifespan events defined in your FastAPI application. If your application relies on lifespan events, use LifespanManager from the florimondmanca/asgi-lifespan package to ensure they are triggered during testing.

RuntimeError when event loop objects are shared across async boundaries

If you encounter RuntimeError: Task attached to a different loop when integrating asynchronous function calls in tests (such as when using MongoDB MotorClient), instantiate objects that need an event loop only within async functions, such as in @app.on_event(startup) callbacks, rather than at module level.

Async test functions can await other async operations

When a test function is async (marked with @pytest.mark.anyio), you can call and await other asynchronous functions within it, such as async database operations, in addition to making async requests to your FastAPI application.

FastAPI applications are async underneath even with def functions

Even if a FastAPI application uses normal def functions for routes instead of async def, the FastAPI application itself is an asynchronous ASGI application underneath. The TestClient uses special handling to call this async app from synchronous test functions, but this magic does not work when tests themselves are async.

app.dependency_overrides attribute for testing

FastAPI applications have an attribute called app.dependency_overrides which is a dictionary. To override a dependency during testing, set the key to the original dependency function and the value to the override dependency function. FastAPI will then call the override function instead of the original.

Resetting dependency overrides

To remove all dependency overrides and reset to the original dependencies, set app.dependency_overrides to an empty dictionary using app.dependency_overrides = {}.

Dependency overrides apply everywhere

You can set a dependency override for a dependency used anywhere in a FastAPI application, including in path operation functions, path operation decorators, .include_router() calls, and other locations. FastAPI will still be able to override it.

Scoped dependency overrides during tests

To override a dependency only during some tests, set the override at the beginning of the test function and reset it at the end of the test function. This allows you to apply different overrides to different tests.

Use case for dependency overrides: external services

A common use case for dependency overrides is replacing calls to expensive external services during testing. For example, you can override a dependency that calls an external authentication provider with a custom dependency that returns a mock user, avoiding the cost and latency of the external service during test runs.

TestClient for WebSocket testing

The TestClient can be used to test WebSockets. Use the TestClient in a `with` statement to connect to the WebSocket.

Give your agent this brain