liveBook · Manning

assertEqual

As explained in Django in Action

assertEqual is a Python test method that checks whether two values are equal and reports a failed assertion otherwise.

assertEqual is a Python TestCase method for checking that two values match. If they differ, it raises an exception and marks the test as failed. section 8.2

In a Django test, compare an expected result with a response value, commonly after the test client requests a URL. Putting 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) The test expects the credits view to return HTTP 200. Changing 200 to 42 produces a failure such as AssertionError: 42 != 200, with a traceback identifying the test line. 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 case to connect assertEqual with TestCase inheritance, the Django test client, URL routing, and response status codes. section 8.2

How Django in Action covers it

How deep it goes
Explains equality assertions, argument order, exceptions, tracebacks, and failed test output without surveying every assertion method.
Examples
Tests the credits view at /credits/ by comparing response.status_code with 200, then demonstrates failure with 42.
What it assumes
Assumes a Django TestCase and test client, while demonstrating assertion syntax and response status checking directly.
What you can do after
Diagnose view-test failures by comparing expected values with response data and reading the resulting traceback.
How it connects
Connects Python TestCase, Django test client, URL routing, HTTP status codes, and the Django test runner.

Also explained in

Compare how 2 books explain assertEqual →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.