10

When I use assertEqual() with two dictionaries in REPL, it shows me a diff, e.g.:

>>> import unittest
>>> class A(unittest.TestCase):
...   pass
... 
>>> a = A()
>>> d1 = dict(zip(range(10), range(1000000, 1000010)))
>>> d2 = dict(zip(range(3, 13), range(1000003, 1000013)))
>>> a.assertEqual(d1, d2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/unittest/case.py", line 820, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.5/unittest/case.py", line 1111, in assertDictEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/usr/lib/python3.5/unittest/case.py", line 665, in fail
    raise self.failureException(msg)
AssertionError: {0: 1000000, 1: 1000001, 2: 1000002, 3: 10[73 chars]0009} != {3: 1000003, 4: 1000004, 5: 1000005, 6: 10[76 chars]0012}
- {0: 1000000,
-  1: 1000001,
-  2: 1000002,
-  3: 1000003,
? ^

+ {3: 1000003,
? ^

   4: 1000004,
   5: 1000005,
   6: 1000006,
   7: 1000007,
   8: 1000008,
-  9: 1000009}
?            ^

+  9: 1000009,
?            ^

+  10: 1000010,
+  11: 1000011,
+  12: 1000012}

When I do the same in a Django unit test, sometimes it prints a diff and sometimes only the first shortened line. I wonder, how can I make it always print the diff.
I run Django tests with ./manage.py test -v 3.

planetp
  • 14,248
  • 20
  • 86
  • 160
  • Does this answer your question? [How to set self.maxDiff in nose to get full diff output?](https://stackoverflow.com/questions/14493670/how-to-set-self-maxdiff-in-nose-to-get-full-diff-output) – ggorlen Sep 27 '21 at 22:17

2 Answers2

8

If you set maxDiff to None, then the full diff will be shown.

class A(unittest.TestCase):
    maxDiff = None
    ...
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • 6
    I've tried setting this on the class and instance levels, still no diff :( – planetp May 12 '16 at 09:03
  • Make sure you have the capitalization correct. Apart from that, I'm afraid I don't have any ideas why it wouldn't work. – Alasdair May 12 '16 at 09:10
  • i've also tried setting `maxDiff = None` in a case derived from `unittest.TestCase` and nothing's changed – mburke05 Jul 07 '18 at 15:17
  • 1
    I ended up using assertDictEqual. It seems that `_truncateMessage` uses maxDiff field value, but not all assert methods use `_truncateMessage`. – Juuso Ohtonen Sep 24 '19 at 04:40
3

An old question but worth an answer for anyone else reading this now, like I was!

You should set maxDiff = None but also use assertDictEqual() instead of assertEqual().

The reason setting maxDiff = None didn't work for the OP was that they were using assertEqual(). maxDiff = None only affects assertSequenceEqual(), assertDictEqual() and assertMultiLineEqual() in unittest, see the docs.

DrBuck
  • 822
  • 7
  • 22
  • 1
    `maxDiff = None` affects `assertEqual` too: `self.assertEqual("a" * 800, "b" * 800)` gives the `Diff is 1607 characters long. Set self.maxDiff to None to see it.` message, then adding `unittest.TestCase.maxDiff = None` shows the full diff and hides the warning. – ggorlen Sep 27 '21 at 22:32
  • Thank you, this was why maxDiff wasn't having an effect for me – Eloff Aug 24 '22 at 07:17