0Pricing
Django Academy · Lesson

Writing TestCase and assertions

Test models and views automatically.

Writing TestCase and assertions is a free Django Academy lesson on CoddyKit — lesson 3 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 Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Writing TestCase and assertions” lesson free?

Yes — the full text of “Writing TestCase and assertions” is free to read here on the web, and the Django 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 Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “Writing TestCase and assertions”?

Test models and views automatically. You practise Django 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 Django Academy?

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

How long does the “Writing TestCase and assertions” 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 Django Academy lesson?

Yes. Every Django 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 Paginator Class
  2. Page Controls in ListView
  3. Writing TestCase and assertions
  4. The Test Client and Fixtures
← Back to Django Academy