0Pricing
Flask Academy · Lesson

The Test Client and Fixtures

Spin up an app and client for tests.

The Test Client and Fixtures is a free Flask Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Test Your Flask App

Tests let you change code without fear. With pytest you can prove every route still works in seconds, instead of clicking through your app by hand. 🧪

Meet the Test Client

Flask ships a fake browser called the test client. It calls your routes in memory, with no real server or network involved.

client = app.test_client()
resp = client.get('/')

Get a Client From Your App

You build the client by calling app.test_client(). It returns an object whose methods mirror real HTTP verbs like get and post.

from app import app
client = app.test_client()

Send a Request, Get a Response

Calling client.get('/') returns a response object. You inspect it just like a real one, but everything ran instantly inside Python.

resp = client.get('/')
print(resp.status_code)

What Is a Fixture

A fixture is reusable setup code pytest runs before a test. Instead of rebuilding the client everywhere, you define it once and reuse it.

Write a Client Fixture

Decorate a function with @pytest.fixture and yield a test client. Any test that names it as an argument receives a fresh client.

@pytest.fixture
def client():
    yield app.test_client()

Request the Fixture by Name

To use a fixture, simply add its name as a test argument. Pytest sees the match and injects the value for you automatically.

def test_home(client):
    resp = client.get('/')
    assert resp.status_code == 200

Yield vs Return in Fixtures

Using yield lets you run cleanup after the test finishes. Code before yield is setup; code after yield tears everything down.

@pytest.fixture
def client():
    yield app.test_client()
    print('cleanup here')

Where to Put Fixtures

Put shared fixtures in a file named conftest.py. Pytest finds it automatically, so every test file can use those fixtures with no import.

Naming Test Files

Pytest discovers files that start with test_ and functions named the same way. Stick to that pattern and your tests run with one command.

# file: test_routes.py
def test_home(client):
    ...

Run the Whole Suite

From your project root, just run pytest in the terminal. It collects every test file, runs them, and prints a clean pass or fail summary.

$ pytest
2 passed in 0.04s

Quick Check

You want a reusable test client. How do you do it?

Recap: Client and Fixtures

You met the in-memory test client and learned to share it with a fixture in conftest.py. Now every test starts with a ready client. 🎉

Frequently asked questions

Is the “The Test Client and Fixtures” lesson free?

Yes — the full text of “The Test Client and Fixtures” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “The Test Client and Fixtures”?

Spin up an app and client for tests. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Test Client and Fixtures” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Test Client and Fixtures
  2. Assert on Routes and JSON
  3. Isolate Tests with a Test Database
  4. Test Authenticated Endpoints
← Back to Flask Academy