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 · Reference · all subjects

testing

9 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 import location

The TestClient class can be imported directly from fastapi.testclient using the statement 'from fastapi.testclient import TestClient'.

TestClient purpose

The TestClient class is used to test FastAPI applications without creating an actual HTTP and socket connection. It communicates directly with the FastAPI code instead.

Using pytest.mark.anyio for async tests

To run async test functions in pytest, use the @pytest.mark.anyio decorator on your test function. This tells pytest to call the test function asynchronously. The test function itself must be defined with async def rather than just def.

AsyncClient for testing FastAPI applications

When writing async tests, use AsyncClient from HTTPX instead of TestClient. Create an AsyncClient instance with the app, and use await when sending requests. Example: async with AsyncClient(app=app, base_url="http://test") as client: response = await client.get("/").

TestClient does not work in async test functions

The TestClient uses internal magic to call the async FastAPI application from normal synchronous def test functions. This magic does not work when the test function itself is async. Therefore, TestClient cannot be used in async test functions; use AsyncClient instead.

TestClient is based on HTTPX

The TestClient is built on top of HTTPX. You can use HTTPX directly when testing, particularly when you need to write async tests.

AsyncClient does not trigger lifespan events

If your FastAPI application relies on lifespan events (startup/shutdown), the AsyncClient does not trigger these events. To ensure lifespan events are triggered, use LifespanManager from florimondmanca/asgi-lifespan instead.

Other async function calls in async tests

When using async def for test functions, you can call and await other async functions within the test, such as asynchronous database queries or other async library calls, just as you would in other parts of your code.

RuntimeError with Task attached to a different loop

If you encounter RuntimeError: Task attached to a different loop when calling async functions in tests (for example with MongoDB's MotorClient), ensure that objects requiring an event loop are instantiated only within async functions, such as in @app.on_event("startup") callbacks, rather than at module level.

Give your agent this brain