Introduction
Testing Django apps helps catch mistakes early and makes sure your app works as expected. It saves time and keeps users happy.
Jump into concepts and practice - no test required
Testing Django apps helps catch mistakes early and makes sure your app works as expected. It saves time and keeps users happy.
from django.test import TestCase class MyTest(TestCase): def test_example(self): self.assertEqual(1 + 1, 2)
Tests are written as classes that inherit from django.test.TestCase.
Each test is a method starting with test_ that checks one thing.
from django.test import TestCase class SimpleTest(TestCase): def test_addition(self): self.assertEqual(2 + 2, 4)
from django.test import TestCase class ViewTest(TestCase): def test_homepage_status(self): response = self.client.get('/') self.assertEqual(response.status_code, 200)
This test class checks simple math operations to show how tests confirm expected results.
from django.test import TestCase class MathTest(TestCase): def test_multiply(self): result = 3 * 5 self.assertEqual(result, 15) def test_divide(self): result = 10 // 2 self.assertEqual(result, 5)
Running tests often helps find errors early.
Tests also document how your app should behave.
Use Django's test client to simulate user actions like visiting pages.
Testing keeps your Django app reliable and bug-free.
Write small tests for each feature or bug fix.
Run tests regularly during development and before releases.
python manage.py test runs all tests in the project.from django.test import TestCase
class SimpleTest(TestCase):
def test_addition(self):
self.assertEqual(2 + 2, 4)from django.test import TestCase
class MyTest(TestCase):
def test_example(self):
self.assertTrue(True)self.assertTrue(True) is not indented inside the method properly.