Keploy logo
FastAPI logo

Keploy as a FastAPI testing framework

Keploy tests FastAPI applications by recording real requests through the running ASGI server and every async database or HTTP call they trigger, then replaying both as assertions. There are no dependency_overrides to register and no TestClient to construct.

Generate FastAPI tests free
keploy record -c "uvicorn app.main:app --port 8000"
18.4K+VS Code1.2M+300M+mocks created

What Keploy gives a FastAPI team

Any FastAPI app, unchanged

Keploy runs the app through uvicorn, hypercorn, or gunicorn with uvicorn workers. Routers, sub-applications, and lifespan handlers all execute because capture happens at the socket rather than inside Starlette.

  • FastAPI 0.95 and above
  • uvicorn, hypercorn, or gunicorn
  • Routers and mounted sub-apps
  • Lifespan startup actually runs
The problem

Why FastAPI integration tests slow teams down

The friction is rarely the assertions. It is everything around them — spinning up dependencies, keeping mocks honest, and repairing tests after every refactor.

Three ways to do it

Testing FastAPI: by hand, with pytest + TestClient, or with Keploy

TestClient drives the app in-process with providers swapped out. Keploy records the running uvicorn server, so the app under test is the one you deploy and the real Depends chain executes.

Select any row for the full comparison, with code.

Same coverage, three costs

What you write for FastAPI vs what Keploy records

All three produce the same assertion. Only the third still passes after the next refactor without anyone editing it.

By hand2–4 hours
test_orders_manual.py
hand-written
# Hand-written: stubs injected, no server involved.
from app.services import OrderService
 
class FakeOrderRepo:
async def get(self, order_id):
return {"id": order_id, "amount_minor": 4200, "state": "NEW"}
 
class FakePayments:
async def authorize(self, token):
return {"status": "AUTHORIZED"}
 
async def test_confirm_order():
service = OrderService(FakeOrderRepo(), FakePayments())
 
result = await service.confirm("ord_1", "tok_123")
 
assert result["state"] == "CONFIRMED"
assert result["amount_minor"] == 4200

The route, its Depends chain, and Pydantic response validation are all absent — this only proves the service function works with the shapes you typed.

pytest + TestClient1–2 hours
test_orders_testclient.py
tool-assisted
# pytest + TestClient + dependency_overrides + respx.
import respx
from fastapi.testclient import TestClient
from app.main import app, get_repo, get_payments
 
app.dependency_overrides[get_repo] = lambda: FakeOrderRepo()
app.dependency_overrides[get_payments] = lambda: FakePayments()
 
client = TestClient(app)
 
@respx.mock
def test_confirm_order():
respx.post(
"https://api.payments.example.com/v1/payment_intents/tok_123/confirm"
).respond(200, json={"status": "AUTHORIZED"})
 
response = client.post(
"/orders/ord_1/confirm", json={"payment_token": "tok_123"}
)
 
assert response.status_code == 200
assert response.json()["state"] == "CONFIRMED"

Idiomatic and fast — and both providers are overridden, so the real repository and payment client are never exercised by this test at all.

With Keploy~5 minutes
test-1.yaml
auto-generated
# Recorded with: keploy record -c 'uvicorn app.main:app --port 8000'
# The real Depends chain ran. No overrides.
version: api.keploy.io/v1beta1
kind: Http
name: test-1
spec:
req:
method: POST
url: /orders/ord_1/confirm
header:
Authorization: Bearer <captured>
body: '{"payment_token":"tok_123"}'
resp:
status_code: 200
body:
id: "ord_1"
state: "CONFIRMED"
amount_minor: 4200
confirmed_at: "2026-09-02T11:04:18Z"
noise:
- body.confirmed_at
mocks:
- kind: Postgres
operation: "SELECT id, amount_minor, state FROM orders WHERE id = $1"
- kind: Http
url: https://api.payments.example.com/v1/payment_intents/tok_123/confirm
status: 200

Pydantic validation, the auth dependency, and the real asyncpg query all ran on the way to this response — none of which a dependency_overrides test covers.

Times are estimates for authoring one endpoint’s coverage from scratch, not measurements.

Keploy vs the alternatives

FastAPI testing tools, compared

The options a team on Python actually reaches for, and where each one genuinely wins. Select a row for the full comparison.

Best in classStrongPartialNot covered

Assessments reflect each tool’s documented behaviour, not benchmark measurements.

How it works

Record your FastAPI app once, replay it forever

Keploy sits below your FastAPI process at the network layer. It watches the calls your app already makes, then serves them back on replay so tests run with no dependencies attached.

Keploy records a GET call to /api/v1/orders/{id} on a FastAPI service and captures the dependency calls it makes.

FastAPI logo
Your FastAPI app
GET/api/v1/orders/{id}200
RecordingKeploy proxyeBPF · userspace
+3 more FastAPI dependencies
An example shape of a captured call. Your own endpoints and dependencies come from your real traffic, so nothing here has to be written by hand.
Mock coverage

What Keploy mocks for FastAPI, with zero config

7 of the 7 dependencies a typical FastAPI service talks to are stubbed from the recording itself — no mock classes, no fixture files, no containers in CI.

Quick start

Your first FastAPI test suite in under five minutes

Every command below runs against your existing FastAPI service. Nothing in your source tree changes.

  1. 1Install the Keploy CLI

    A single binary. It needs a Linux kernel with eBPF support, or Docker on macOS and Windows — and it adds nothing to your project's dependencies.

    curl -sSL https://keploy.io/install.sh | bash
  2. 2Record your service

    Pass the command you already use to start the app. Keploy runs it and watches every socket it opens.

    keploy record -c "uvicorn app.main:app --port 8000"
  3. 3Exercise the paths you care about

    Drive the running server. Every request becomes a test case with its async database and httpx calls captured alongside.

    curl -X POST localhost:8000/orders/ord_1/confirm -H 'Content-Type: application/json' -H 'Authorization: Bearer $TOKEN' -d '{"payment_token":"tok_123"}'
  4. 4Replay in CI

    Replay serves the recorded dependency responses, so the job needs no service containers and no Docker daemon.

    keploy test -c "uvicorn app.main:app --port 8000" --delay 10

Ready to try it on your own FastAPI service?

Ecosystem

Works with the rest of your FastAPI stack

Keploy records at the network layer, so framework and driver choices inside your FastAPI app do not change how it captures traffic.

FAQ

FastAPI testing with Keploy: common questions

Join our GlobalCommunity

Connect with developers worldwide. Follow updates, ask questions, share feedback, and ship faster with other Keploy builders.

1.2M+Installs
18.4K+GitHub
100K+Devs
300M+Mocks
1K+Contributors
#1OSS Trending
4.9★★★★★from 500+ reviews onG2GartnerVS CodeChrome
★★★★★

Best report of integration and API tests I've seen — which we don't get from RestAssured.

G2
★★★★★

Future of microservices testing. I don't write tests now!

G2 · 5/5
★★★★★

An amazing product that simplifies the automation.

Gartner · 4.0
XGitHubSlackYouTubeLinkedIn
Built by developers, for developers.Let's build the future, together.