pytest-flask
02 / 02

pytest-flask Fundamentals: client, app & Assertions

pytest-flask: client, app & Assertions

pytest-flask is a pytest plugin providing fixtures and utilities for testing Flask applications -- reducing boilerplate compared to manually setting up a Flask test client in every test.

The app and client Fixtures

# conftest.py
@pytest.fixture
def app():
    app = create_app()
    app.config["TESTING"] = True
    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
    return app

# test_users.py -- 'client' fixture is provided by pytest-flask,
# wrapping Flask's own test client
def test_get_user(client):
    response = client.get("/users/1")
    assert response.status_code == 200
    assert response.json["name"] == "Ada"

Why TESTING = True Matters

Flask's TESTING config flag enables test-friendly behavior -- like propagating exceptions to the test runner instead of returning a generic 500 error page -- making test failures easier to diagnose.

Why Test Through the HTTP Client

Testing via the actual request/response cycle exercises routing, view logic, and response formatting together, catching integration problems (a misconfigured route, a missing header) that testing an isolated function wouldn't necessarily surface.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free