Why is assertDictEqual needed if dicts can be compared by `==`?

This is part of a broader question:

Why does unittest have all the special asserts at all?

The answer is that the primary job of the UnitTest assert* methods is to give you meaningful output when a test fails. Take a look at the unittest module code -- that really is mostly what they do (only what they do?)

Given that Python is a dynamic language with easy introspection, why bother will all that? And the answer is "because unittest was ported from the Java junit package, and that's how they did it in Java" (and probably had to, given how much harder or impossible it is to introspect at run time).

So my recommendation: unless you are writing tests for the standard library, don't use unittest at all -- all it does is get in the way. I use pytest. nose may be a good option as well. It make it quicker and easier to write tests, and you get excellent reporting when you get errors.

It also includes lots of nifty features for parameterized testing, fixtures, test configuration, mocking, etc...

If you are on a project that already uses unittest -- you still can run your tests with pytest, and get many of its advantages.


Basically, it allows unittest to give you more information about why the test failed ("diagnostics", to use the language from "Growing Object-Oriented Software Guided by Tests" by Steve Freeman and Nat Pryce). Compare these two tests:

class DemoTest(unittest.TestCase):

    D1 = {'a': 1, 'b': 2, 'c': [1, 2]}
    D2 = {'a': 1, 'b': 2, 'c': [1]}

    def test_not_so_useful(self):
        self.assertTrue(self.D1 == self.D2)

    def test_useful(self):
        self.assertDictEqual(self.D1, self.D2)

And their outputs:

Failure
Traceback (most recent call last):
  File "...x.py", line 86, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true

vs.

Failure
Traceback (most recent call last):
  File "...x.py", line 80, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'c': [1, 2], 'b': 2} != {'a': 1, 'c': [1], 'b': 2}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]}

In the latter, you can see exactly what the difference was, you don't have to work it out yourself. Note that you can just use the standard assertEqual instead of assertDictEqual, with the same result; per the docs

...it’s usually not necessary to invoke these methods directly.