Testing
- Learn how to write and execute unit tests in Django for reliable applications.
What is Testing?
Testing means checking whether your application works correctly before deploying it to production.
In Django:
Testing ensures that models, views, forms, and logic behave as expected.
Why Testing is Important?
Finds bugs early
Prevents future errors
Improves code quality
Required in professional projects
Saves time and cost
Types of Testing in Django
Django Testing Tools
Built-in Django Testing Framework
Django uses Python’s unittest module.
Key classes:
TestCase
SimpleTestCase
TransactionTestCase
Client
Django Test Command
python manage.py test
Automatically:
Creates test database
Runs tests
Destroys database after test
Writing Test Cases
Test File Structure
app/
├── tests.py
or
app/
├── tests/
│ ├── test_models.py
│ ├── test_views.py
│ ├── test_forms.py
Basic Test Case Structure
Basic Test Case Example
tests.py
from django.test import TestCase
class SampleTest(TestCase):
def test_example(self):
self.assertEqual(2 + 2, 4)
Description
TestCase → Base class
Methods must start with test_
Uses assertion methods
Model Testing
Test Database Automatically Used
Django creates a temporary database for tests.
Model Test Case
tests/test_models.py
from django.test import TestCase
from .models import Student
class StudentModelTest(TestCase):
def test_student_creation(self):
student = Student.objects.create(
name="Rahul",
email="rahul@test.com",
course="Django"
)
self.assertEqual(student.name, "Rahul")
Description
Tests model object creation
Uses test database
View Testing
Django Test Client
Simulates a browser.
View Test Using Client
tests/test_views.py
from django.test import TestCase
from django.urls import reverse
class HomeViewTest(TestCase):
def test_home_status_code(self):
response = self.client.get(reverse('home'))
self.assertEqual(response.status_code, 200)
Description
self.client.get() → fake HTTP request
reverse() → URL name lookup
Test View Template
Template Test
def test_home_template_used(self):
response = self.client.get(reverse('home'))
self.assertTemplateUsed(response, 'home.html')
Form Testing
Form Validation Test
from django.test import TestCase
from .forms import StudentForm
class StudentFormTest(TestCase):
def test_valid_form(self):
form = StudentForm(data={
'name': 'Amit',
'email': 'amit@test.com',
'course': 'Python'
})
self.assertTrue(form.is_valid())
- Authentication Testing
Login Test Case
from django.contrib.auth.models import User
class LoginTest(TestCase):
def test_user_login(self):
user = User.objects.create_user(
username='testuser',
password='12345'
)
login = self.client.login(
username='testuser',
password='12345'
)
self.assertTrue(login)
Middleware Testing
Middleware Test
class MiddlewareTest(TestCase):
def test_login_required_redirect(self):
response = self.client.get('/dashboard/')
self.assertEqual(response.status_code, 302)
API Testing
JSON Response Test
def test_api_response(self):
response = self.client.get('/api/students/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['status'], 'success')
Common Assertions in Django