Testing Views
In this page:
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.
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.
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.
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. #}
- Forgetting that self.client.get() returns a response object, and trying to inspect the HTML without checking response.status_code first.
- Hardcoding a URL string instead of using reverse() with the URL name, which breaks silently if the URL pattern changes.
- Not testing both the success case and an edge case like a missing object (404).
- 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.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: