from django.test import Client, TestCase
class BasicTestClient(TestCase):
def setUp(self):
self.client = Client()
def test_homepage(self):
response = self.client.get("/")
self.assertEqual(response.status_code, 200)
The Client
instance behaves like a lightweight browser, allowing GET, POST, PUT, DELETE, and PATCH requests.
class GetRequestTest(TestCase):
def test_get_homepage(self):
response = self.client.get("/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Welcome")
assertContains(response, "Welcome")
ensures the response contains specific content.assertEqual(response.status_code, 200)
confirms the page loads correctly.class PostRequestTest(TestCase):
def test_post_product(self):
response = self.client.post("/add-product/", {"name": "Laptop", "price": 1200})
self.assertEqual(response.status_code, 302) # Redirect after success
client.post(url, data, format)
simulates form submissions.302
indicates a redirect, often after successful form submission.from django.contrib.auth.models import User
class AuthTestCase(TestCase):
def setUp(self):
self.user = User.objects.create_user(username="testuser", password="securepassword")
def test_login(self):
login = self.client.login(username="testuser", password="securepassword")
self.assertTrue(login)
client.login(username, password)
logs in a user without using the UI.class ProtectedViewTest(TestCase):
def test_protected_page_redirects(self):
response = self.client.get("/dashboard/")
self.assertEqual(response.status_code, 302) # Redirect to login page
class AuthenticatedUserTest(TestCase):
def setUp(self):
self.user = User.objects.create_user(username="testuser", password="securepassword")
self.client.login(username="testuser", password="securepassword")
def test_dashboard_accessible(self):
response = self.client.get("/dashboard/")
self.assertEqual(response.status_code, 200)
For Django Rest Framework (DRF), use APITestCase
.
from rest_framework.test import APITestCase
from rest_framework import status
class APIClientTest(APITestCase):
def test_get_products(self):
response = self.client.get("/api/products/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
Use HTTP_
prefix for headers in requests.
class HeaderTestCase(TestCase):
def test_custom_header(self):
response = self.client.get("/", HTTP_USER_AGENT="Mozilla/5.0")
self.assertEqual(response.status_code, 200)
Sessions can be modified directly using client.session
.
class SessionTestCase(TestCase):
def test_session_variable(self):
session = self.client.session
session["cart_items"] = 5
session.save()
response = self.client.get("/cart/")
self.assertContains(response, "Items in Cart: 5")
follow=True
for Redirectsclass RedirectTest(TestCase):
def test_redirect(self):
response = self.client.get("/dashboard/", follow=True)
self.assertRedirects(response, "/accounts/login/?next=/dashboard/")
follow=True
allows automatic following of redirects.Django’s test client disables CSRF protection by default. To enable it:
from django.test import Client
class CSRFSecurityTest(TestCase):
def setUp(self):
self.client = Client(enforce_csrf_checks=True)
def test_csrf_protected_post(self):
response = self.client.post("/submit-form/", {"name": "John"})
self.assertEqual(response.status_code, 403) # CSRF token missing
enforce_csrf_checks=True
forces CSRF validation.from django.core.files.uploadedfile import SimpleUploadedFile
class FileUploadTest(TestCase):
def test_upload_image(self):
file = SimpleUploadedFile("test.jpg", b"file_content", content_type="image/jpeg")
response = self.client.post("/upload/", {"image": file})
self.assertEqual(response.status_code, 200)
SimpleUploadedFile
mocks file uploads in tests.Use LiveServerTestCase
for real HTTP requests.
from django.test import LiveServerTestCase
import requests
class LiveServerTest(LiveServerTestCase):
def test_live_server(self):
response = requests.get(self.live_server_url + "/")
self.assertEqual(response.status_code, 200)