tezvyn:

How do you test a FastAPI GET endpoint with pytest and TestClient?

AI-drafted, machine-checkedSource: fastapi.tiangolo.combeginner
WHAT IT TESTS

FastAPI testing with TestClient.

ANSWER OUTLINE

Import TestClient and app, write a test_ function, call client.get("/items/1"), assert status_code == 200 and json() matches expected data.

RED FLAG

Using requests or forgetting test_ prefix.

WHAT THIS TESTS: This question checks whether you have actually wired up a FastAPI test suite or only read the docs. The interviewer wants to see that you know TestClient lives in fastapi.testclient, that you understand pytest discovery rules, and that you treat the test as an HTTP-level contract test rather than a unit test of the function body. It also reveals whether you know how to inspect the response object for both status and payload, and whether you understand that TestClient runs the app in-process without starting a server.

A GOOD ANSWER COVERS: A strong answer hits four things in order. First, import TestClient from fastapi.testclient and import the app instance from the application module. Second, define a function with the test_ prefix so pytest picks it up automatically. Third, instantiate TestClient with the app and call client.get with a concrete path parameter such as items/42. Fourth, make two explicit assertions: one that response.status_code equals 200 and another that response.json() returns the expected dictionary or list. Mentioning that TestClient is synchronous by default and based on HTTPX is a nice bonus.

COMMON WRONG ANSWERS: Watch out for three red flags. One, importing requests and trying to hit a running server instead of using TestClient, which spins up the app in-process. Two, asserting on the response object itself rather than its status_code or json() method. Three, forgetting the test_ prefix on the function name so pytest silently skips the file. A subtler mistake is creating a brand new FastAPI() instance inside the test file rather than importing the real app, which means the test exercises an empty application. Another pitfall is installing httpx but forgetting to import TestClient from fastapi.testclient.

LIKELY FOLLOW-UPS: The interviewer will probably stretch the scenario in one of three directions. They might ask how you would test a 404 response for an invalid item_id. They might ask how to override dependencies such as database sessions or authentication inside tests. They might also ask about async testing with AsyncClient or how to run the test database setup and teardown around the TestClient calls.

ONE CONCRETE EXAMPLE: Imagine an app defined in main.py with a get endpoint at items/item_id that returns item_id and name. In test_main.py you would write from fastapi.testclient import TestClient, from main import app, then define test_read_item. Inside that function create client = TestClient(app), then response = client.get("/items/42"). Next assert response.status_code == 200 and assert response.json() == {"item_id": 42, "name": "Foo"}. That is the full pattern the interviewer expects to hear.

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.