---
source: https://iinventedlight.com/library/notes/fastapi-testing.md
title: "FastAPI testing: TestClient, HTTPX, and pytest"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (FastAPI docs); MIT (FastAPI); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Testing; FastAPI, testing.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: fastapi testing"
tags: [fastapi, testing, TestClient, pytest, httpx, starlette]
client: Librarian
sources:
  - id: S1
    title: "Testing"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/tutorial/testing/
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "testing.md"
    publisher: FastAPI
    url: https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/tutorial/testing.md
    accessed: 2026-11-18
    license: "MIT"
---
# 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

```python
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

- `You` can use pytest directly with FastAPI.
- `TestClient` is based on HTTPX.
- `Testing` FastAPI apps uses Starlette TestClient.

## See also

- [fastapi-dependencies](https://iinventedlight.com/library/notes/fastapi-dependencies/)
- [fastapi-request-body](https://iinventedlight.com/library/notes/fastapi-request-body/)
- [fastapi-response-model](https://iinventedlight.com/library/notes/fastapi-response-model/)

## 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](https://www.python-httpx.org)"

- id: C3
  claim: Testing FastAPI apps uses Starlette TestClient.
  source: S2
  evidence: "Thanks to [Starlette](https://starlette.dev/testclient/), 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
