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 · Tutorial · 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.

TestClient import and setup

TestClient is imported from fastapi.testclient (or equivalently from starlette.testclient). You create a TestClient by passing your FastAPI application instance to it. TestClient requires httpx to be installed as a dependency.

Testing functions use regular def, not async def

Testing functions should be normal def functions, not async def functions. Calls to the TestClient are also normal calls, not using await. This allows you to use pytest directly without complications.

pytest test function naming convention

Test functions must have names that start with 'test_' to follow the standard pytest convention and be automatically discovered and executed.

TestClient behaves like httpx

TestClient is based on and behaves the same as httpx, which is in turn based on the Requests library. You can use TestClient the same way you would use httpx or requests.

Passing path and query parameters in tests

To pass path or query parameters in tests, add them to the URL itself, the same way you would construct the URL when making a request.

Passing JSON body in tests

To pass a JSON request body in tests, pass a Python object (such as a dict) to the json parameter of the TestClient method.

Passing form data in tests

To send Form Data instead of JSON in tests, use the data parameter instead of the json parameter.

Passing headers in tests

To pass headers in tests, use a dict in the headers parameter of the TestClient method.

Passing cookies in tests

To pass cookies in tests, use a dict in the cookies parameter of the TestClient method.

TestClient receives JSON-convertible data, not Pydantic models

TestClient receives data that can be converted to JSON, not Pydantic model instances directly. If you have a Pydantic model in your test and want to send its data to the application, use jsonable_encoder to convert it first.

Test file location in larger applications

In applications with multiple modules, test files can be placed in the same Python package (directory with __init__.py) as the code being tested, allowing relative imports to import the app object.

Running pytest from command line

Run tests with the command 'pytest' (or 'uv run pytest' if using uv). pytest will automatically detect files and tests, execute them, and report results.

Async tests in FastAPI

For testing async functions apart from sending requests to the FastAPI application (such as asynchronous database functions), refer to the Async Tests section in the advanced tutorial.

Give your agent this brain