liveBook · Manning

assertEqual

As explained in Python How-To

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

assertEqual is a unittest.TestCase assertion that compares an actual value with an expected value. If the values are equal, the test passes; otherwise, unittest reports an AssertionError. section 13.3.2 section 13.5

Compare a created object with the expected result

class TestTaskCreation(unittest.TestCase): def test_create_task(self): task_text = "Laundry,3" created_task = create_task(task_text) self.assertEqual( created_task.__dict__, Task("Laundry", 3).__dict__ ) Here, assertEqual compares the created task's attribute dictionary with the expected task's dictionary. A successful run reports OK. section 13.3.2

Listing 13.5 Testing a function using TestCase (test_task_func.py)
from task_func import Task, create_task         #1
import unittest                                 #2
 
class TestTaskCreation(unittest.TestCase):      #3
    def test_create_task(self):
        task_text = "Laundry,3"
        created_task = create_task(task_text)   #4
        self.assertEqual(created_task.__dict__, 
 Task("Laundry", 3).__dict__)
 
 
if __name__ == "__main__":
    unittest.main()

Use the complete listing to see assertEqual inside a TestCase subclass, alongside test discovery and unittest.main(). section 13.3.2

Use assertEqual for expected-versus-generated results, including function returns and method output. When it fails, compare the displayed values and correct either the implementation or the expected value. section 13.3.2 section 13.4.2

How Python How-To covers it

How deep it goes
Explains equality checks, successful runs, and AssertionError diagnostics, while deferring other TestCase assertions to documentation.
Examples
Compares Task objects through their attribute dictionaries and detects a missing space in formatted task output.
What it assumes
Assumes basic functions, classes, unittest.TestCase, test_ naming, and expected-versus-actual testing patterns.
What you can do after
Diagnose test failures by reading AssertionError output and comparing expected values with generated values.
How it connects
Connects assertEqual with unittest, TestCase, test cases, unit testing, AssertionError, and test discovery.

Also explained in

Compare how 2 books explain assertEqual →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.