0Pricing
Django Academy · Lezione

Scrivere TestCase e asserzioni

Testare automaticamente modelli e viste

Scrivere TestCase e asserzioni è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 3 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 Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.

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

Why Write Tests?

Tests catch bugs before your users do. They let you change code with confidence, knowing a green run means nothing broke. ✅

Meet TestCase

Django gives you a TestCase base class. Subclass it and each method that starts with test_ becomes an automatic check.

from django.test import TestCase

Your First Test Class

Group related checks in one class. Here a TestCase subclass will hold all the tests for the Article model.

class ArticleTests(TestCase):
    pass

A Fresh Database

TestCase runs each test in its own transaction and rolls it back afterward. Tests never pollute your real data or each other.

Writing a Test Method

Name every test method with a test_ prefix so Django's runner discovers and executes it for you.

def test_title_is_saved(self):
    ...

Assert Equality

The classic check is assertEqual. It passes when two values match and fails loudly when they differ.

self.assertEqual(article.title, 'Hello')

Assert True or False

For boolean conditions use assertTrue and assertFalse. They make intent crystal clear in your test.

self.assertTrue(article.is_published)

Setting Up Shared Data

Need the same object in many tests? Build it once in setUp, which Django runs before every test method.

def setUp(self):
    self.a = Article.objects.create(title='Hi')

Testing a Model Method

Verify your model behaves as designed. Create an object, call its method, then assert the result is what you expect.

self.assertEqual(str(self.a), 'Hi')

Run the Tests

Kick everything off with the test command. Django finds your test files, runs them, and reports passes and failures.

python manage.py test

Reading the Output

A dot means a pass, an F means a failure, and E means an error. The summary shows exactly which test broke and why.

Quick Check

Django needs to know which methods are tests.

Recap

You wrote a TestCase, used setUp for shared data, checked results with assertEqual and assertTrue, and ran it all with manage.py test. 🎉

Domande Frequenti

La lezione «Scrivere TestCase e asserzioni» è gratuita?

Sì — il testo completo di «Scrivere TestCase e asserzioni» è 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 Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.

Cosa imparerò in «Scrivere TestCase e asserzioni»?

Testare automaticamente modelli e viste Eserciti Django 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 Django Academy?

Non è richiesta alcuna esperienza precedente. Django 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 3 di 4.

Quanto tempo richiede la lezione «Scrivere TestCase e asserzioni»?

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 Django Academy?

Sì. Ogni lezione Django 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. La classe Paginator
  2. Controlli di pagina in ListView
  3. Scrivere TestCase e asserzioni
  4. Il test client e i fixture
← Torna a Django Academy