Keploy logo
Flask logo

Keploy as a Flask testing framework

Keploy tests Flask applications by recording real requests through the running WSGI server and every SQLAlchemy query or outbound call they trigger, then replaying both as assertions. There is no test_client to construct and no app fixture to maintain.

Generate Flask tests free
keploy record -c "gunicorn -b 0.0.0.0:8000 app:app"
18.4K+VS Code1.2M+300M+mocks created

What Keploy gives a Flask team

Any Flask app, unchanged

Keploy runs the app through flask run, gunicorn, or uWSGI. Blueprints, app factories, and extension initialisation all execute because capture happens at the socket rather than inside Werkzeug.

  • Flask 2.0 and above
  • flask run, gunicorn, or uWSGI
  • Blueprints and app factories
  • Extensions initialise for real
The problem

Why Flask 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 Flask: by hand, with pytest + test_client, or with Keploy

test_client dispatches into a testing-mode app. Keploy records the running gunicorn server, so the configuration, extensions, and middleware under test are the ones you actually deploy.

Select any row for the full comparison, with code.

Same coverage, three costs

What you write for Flask 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: the view function is called past the stack.
from app.views import confirm_order
 
class FakeRepo:
def get(self, order_id):
return {"id": order_id, "amount_minor": 4200, "state": "NEW"}
 
def test_confirm_order(monkeypatch):
monkeypatch.setattr("app.views.repo", FakeRepo())
monkeypatch.setattr(
"app.views.payments.authorize",
lambda token: {"status": "AUTHORIZED"},
)
 
result = confirm_order("ord_1", {"payment_token": "tok_123"})
 
assert result["state"] == "CONFIRMED"

Two monkeypatches against module paths, and no request context. Routing, before_request hooks, and error handlers are all absent.

pytest + test_client1–2 hours
test_orders_client.py
tool-assisted
# pytest + test_client + responses.
import pytest
import responses
from app import create_app, db
 
@pytest.fixture
def client():
app = create_app({"TESTING": True, "SQLALCHEMY_DATABASE_URI": TEST_DB})
with app.app_context():
db.create_all()
seed_orders()
yield app.test_client()
db.drop_all()
 
@responses.activate
def test_confirm_order(client):
responses.add(
responses.POST,
"https://api.payments.example.com/v1/payment_intents/tok_123/confirm",
json={"status": "AUTHORIZED"},
)
 
res = client.post("/orders/ord_1/confirm", json={"payment_token": "tok_123"})
 
assert res.status_code == 200

Correct and idiomatic — and it runs against a TESTING-mode app with a different database URI than the one you deploy.

With Keploy~5 minutes
test-1.yaml
auto-generated
# Recorded with: keploy record -c 'gunicorn -b 0.0.0.0:8000 app:app'
# Production config. Real extensions. No test_client.
version: api.keploy.io/v1beta1
kind: Http
name: test-1
spec:
req:
method: POST
url: /orders/ord_1/confirm
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 = %(id)s"
- kind: Http
url: https://api.payments.example.com/v1/payment_intents/tok_123/confirm
status: 200

Recorded under the configuration you deploy, so a setting that only differs in TESTING mode cannot hide a bug from this case.

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

Keploy vs the alternatives

Flask 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 Flask app once, replay it forever

Keploy sits below your Flask 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 Flask service and captures the dependency calls it makes.

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 Flask, with zero config

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

Quick start

Your first Flask test suite in under five minutes

Every command below runs against your existing Flask 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 "gunicorn -b 0.0.0.0:8000 app:app"
  3. 3Exercise the paths you care about

    Drive the running server. Every request becomes a test case with its SQLAlchemy queries and outbound calls captured alongside.

    curl -X POST localhost:8000/orders/ord_1/confirm -H 'Content-Type: application/json' -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 "gunicorn -b 0.0.0.0:8000 app:app" --delay 8

Ready to try it on your own Flask service?

Ecosystem

Works with the rest of your Flask stack

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

FAQ

Flask 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.