FastAPI testing: TestClient, HTTPX, and pytest
Answer: Testing FastAPI applications uses Starlette TestClient and is based on HTTPX. You can use pytest directly with FastAPI. Create a TestClient by passing your FastAPI application to it. Test functions are ordinary def functions, not async def.
Examples
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
client = TestClient(app)
Notes
To use TestClient, first install httpx. Use TestClient the same way as you do with httpx, and write simple assert statements. FastAPI provides the same starlette.testclient as fastapi.testclient just as a convenience; it comes directly from Starlette.
Reference list
Youcan use pytest directly with FastAPI.TestClientis based on HTTPX.TestingFastAPI apps uses Starlette TestClient.
See also
Claims
- id: C1 claim: You can use pytest directly with FastAPI. source: S1 evidence: "you can use pytest directly with FastAPI"
- id: C2 claim: TestClient is based on HTTPX. source: S2 evidence: "It is based on HTTPX"
- id: C3 claim: Testing FastAPI apps uses Starlette TestClient. source: S2 evidence: "Thanks to Starlette, testing FastAPI applications is easy and enjoyable"
Sources
- [S1] Testing. FastAPI docs. https://fastapi.tiangolo.com/tutorial/testing/ . License: MIT. Accessed 2026-11-18.
- [S2] testing.md. FastAPI. https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/tutorial/testing.md . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Testing; FastAPI, testing.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Testing; FastAPI, testing.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors.
Cite this
iinventedlight. 2026. "FastAPI testing: TestClient, HTTPX, and pytest." iinventedlight Library, iil:note:p72rrz3a, version of 2026-11-18. https://iinventedlight.com/library/notes/fastapi-testing/. License: CC-BY-4.0.
@misc{iil_note_p72rrz3a,
author = {{iinventedlight}},
title = {{FastAPI testing: TestClient, HTTPX, and pytest}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/fastapi-testing/},
note = {Permanent ID iil:note:p72rrz3a. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- fastapi-testing.table.json (table; CC-BY-4.0)
- fastapi-testing.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/fastapi-testing.md.