0Pricing
Flask Academy · Lektion

Der Test-Client und Fixtures

Starten Sie für Tests eine App und einen Client.

Der Test-Client und Fixtures ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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

Häufig gestellte Fragen

Ist die Lektion „Der Test-Client und Fixtures“ kostenlos?

Ja — der vollständige Text von „Der Test-Client und Fixtures“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Der Test-Client und Fixtures“?

Starten Sie für Tests eine App und einen Client. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Flask Academy zu starten?

Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Der Test-Client und Fixtures“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?

Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Der Test-Client und Fixtures
  2. Routen und JSON prüfen
  3. Tests mit einer Testdatenbank isolieren
  4. Authentifizierte Endpoints testen
← Zurück zu Flask Academy