0Pricing
Flask Academy · Lezione

Test client e fixture

Avvii un'app e un client per i test.

Test client e fixture è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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

Domande Frequenti

La lezione «Test client e fixture» è gratuita?

Sì — il testo completo di «Test client e fixture» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.

Cosa imparerò in «Test client e fixture»?

Avvii un'app e un client per i test. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flask Academy?

Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Test client e fixture»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flask Academy?

Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Test client e fixture
  2. Verificare route e JSON
  3. Isolare i test con un database di test
  4. Testare gli endpoint autenticati
← Torna a Flask Academy