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

Testing Views

Testing a view means pretending to be a visitor clicking on a page and checking that the page responds the way it should.

Setting up a view to test

Assume a simple view that returns a 200 response with a greeting. Views are tested through the test Client rather than being called as plain functions.

Example: Setting up a view to test

Assume a simple view that returns a 200 response with a greeting. Views are tested through the test Client rather than being called as plain functions.

markup
from django.http import HttpResponse


def hello_view(request):
    return HttpResponse('Hello, world!')
{# 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. #}

Calling the view with the test client

self.client.get() simulates a GET request to a URL and returns a response object, exactly like a real browser would receive.

Note: Use reverse() to build the URL from its name instead of typing the path directly.

Example: Calling the view with the test client

self.client.get() simulates a GET request to a URL and returns a response object, exactly like a real browser would receive.

markup
from django.test import TestCase
from django.urls import reverse


class HelloViewTests(TestCase):
    def test_hello_view_status_code(self):
        response = self.client.get(reverse('hello'))
        self.assertEqual(response.status_code, 200)
{# 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. #}

Checking response content

response.content holds the raw bytes returned by the view, which can be decoded and checked for expected text.

Example: Checking response content

response.content holds the raw bytes returned by the view, which can be decoded and checked for expected text.

markup
class HelloContentTests(TestCase):
    def test_hello_view_message(self):
        response = self.client.get(reverse('hello'))
        self.assertIn(b'Hello, world!', response.content)
{# 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. #}
Common Mistakes
  1. Forgetting that self.client.get() returns a response object, and trying to inspect the HTML without checking response.status_code first.
  2. Hardcoding a URL string instead of using reverse() with the URL name, which breaks silently if the URL pattern changes.
  3. Not testing both the success case and an edge case like a missing object (404).
Chapter Summary
  • Django's test Client simulates browser requests without running a real server.
  • self.client.get(url) and self.client.post(url, data) are the two most common calls.
  • response.status_code and response.content let you assert what the view actually returned.
  • Use reverse(view-name) instead of hardcoded URLs so tests survive URL changes.

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.