Code-Memo

Using the Django Test Client

1. Instantiating the Test Client

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.


2. Making GET Requests

class GetRequestTest(TestCase):
    def test_get_homepage(self):
        response = self.client.get("/")
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Welcome")

3. Making POST Requests

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

4. Testing Views with Authentication

a) Logging in a User
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)
b) Testing Protected Views
class ProtectedViewTest(TestCase):
    def test_protected_page_redirects(self):
        response = self.client.get("/dashboard/")
        self.assertEqual(response.status_code, 302)  # Redirect to login page
c) Accessing a View as a Logged-in User
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)

5. Testing API Endpoints with Django Test Client

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)

6. Sending Custom Headers

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)

7. Handling Sessions with the Test Client

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")

8. Using follow=True for Redirects

class RedirectTest(TestCase):
    def test_redirect(self):
        response = self.client.get("/dashboard/", follow=True)
        self.assertRedirects(response, "/accounts/login/?next=/dashboard/")

9. Testing CSRF Protection

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

10. Uploading Files

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)

11. Running Test Client in Live Server

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)