An assertion method checks a stated condition during a test and raises an exception when the condition is false. In Django, assertEqual() can verify that a view returns the expected HTTP status code. section 8.2
A typical test uses Django’s client to request a URL, then asserts something about the response. For the credits() view, the expected status is 200, meaning the request succeeded. Keeping the expected value first makes failures easier to read. section 8.2
Checking a view response
response = self.client.get("/credits/") self.assertEqual(200, response.status_code) If the test is changed to self.assertEqual(42, response.status_code), Django reports AssertionError: 42 != 200, with a stack trace showing where the failure occurred. section 8.2
Listing 8.1 A test case for the credits() view
# home/tests.py
from django.test import TestCase #1
class TestHome(TestCase): #2
def test_credits(self): #3
response = self.client.get("/credits/") #4
self.assertEqual(200, response.status_code) #5
Use the complete test listing to see the TestCase class, client request, and assertion working together. section 8.2
