0Pricing
Flask Academy · Lekcja

Klient testowy i fixture'y

Uruchamiaj aplikację i klienta na potrzeby testów.

Klient testowy i fixture'y to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Flask Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Flask Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Klient testowy i fixture'y” jest bezpłatna?

Tak — pełny tekst „Klient testowy i fixture'y” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Flask Academy, przejdź na CoddyKit PRO. Kurs Flask Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Klient testowy i fixture'y”?

Uruchamiaj aplikację i klienta na potrzeby testów. Ćwiczysz Flask Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Flask Academy?

Nie wymagamy żadnego doświadczenia. Flask Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.

Ile czasu zajmuje lekcja „Klient testowy i fixture'y”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Flask Academy?

Tak. Każda lekcja Flask Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Klient testowy i fixture'y
  2. Sprawdzanie tras i JSON-a
  3. Izolowanie testów za pomocą testowej bazy danych
  4. Testowanie uwierzytelnionych endpointów
← Powrót do Flask Academy