← Back to Django Course | Chapter 13: Testing in Django | Lesson 3 of 9

Writing Your First Test

Writing your first test is like writing a tiny checklist that tells the computer exactly what 'working correctly' means.

Writing a simple function to test

Suppose a utils.py file defines a function that doubles a number. This is a plain, predictable piece of logic that makes an ideal first test.

Example: Writing a simple function to test

Suppose a utils.py file defines a function that doubles a number. This is a plain, predictable piece of logic that makes an ideal first test.

markup
def double(value):
    return value * 2
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Writing the test method

The test imports the function, calls it with a known input, and asserts the output equals the expected result using assertEqual.

Note: Name the test after the behavior it checks, e.g. test_double_returns_twice_the_value.

Example: Writing the test method

The test imports the function, calls it with a known input, and asserts the output equals the expected result using assertEqual.

markup
from django.test import TestCase
from myapp.utils import double


class DoubleTests(TestCase):
    def test_double_returns_twice_the_value(self):
        self.assertEqual(double(5), 10)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Running the test

python manage.py test runs every discovered test in the project and prints a dot for each pass, or a traceback for each failure.

Example: Running the test

python manage.py test runs every discovered test in the project and prints a dot for each pass, or a traceback for each failure.

bash
python manage.py test

⚠️ Run this command in your terminal.

Common Mistakes
  1. Testing implementation details (internal variable names) instead of observable behavior (return values, output).
  2. Writing a test with no assertion at all, so it always passes no matter what.
  3. Not running the test after writing it, so a typo goes unnoticed.
Chapter Summary
  • A test method calls code with known input and asserts the result using self.assertX methods.
  • Every test should focus on one specific behavior.
  • Run python manage.py test after writing a test to confirm it actually executes and passes.
  • A good first test is simple: check that a function returns what you expect.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.