Commits

Anonymous committed 8b87015

unittest.TestCase.assertDictEqual and assertMultilineEqual provide better default failure messages in the event of long diffs.

Comments (0)

Files changed (2)

Lib/unittest/case.py

         self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary')
 
         if d1 != d2:
+            standardMsg = '%s != %s' % (safe_repr(d1, True), safe_repr(d2, True))
             diff = ('\n' + '\n'.join(difflib.ndiff(
                            pprint.pformat(d1).splitlines(),
                            pprint.pformat(d2).splitlines())))
-            standardMsg = self._truncateMessage('', diff)
+            standardMsg = self._truncateMessage(standardMsg, diff)
             self.fail(self._formatMessage(msg, standardMsg))
 
     def assertDictContainsSubset(self, expected, actual, msg=None):
                 'Second argument is not a string'))
 
         if first != second:
+            standardMsg = '%s != %s' % (safe_repr(d1, True), safe_repr(d2, True))
             diff = '\n' + ''.join(difflib.ndiff(first.splitlines(True),
                                                        second.splitlines(True)))
-            standardMsg = self._truncateMessage('', diff)
+            standardMsg = self._truncateMessage(standardMsg, diff)
             self.fail(self._formatMessage(msg, standardMsg))
 
     def assertLess(self, a, b, msg=None):

Lib/unittest/util.py

 
 __unittest = True
 
+_MAX_LENGTH = 80
+def safe_repr(obj, short=False):
+    try:
+        result = repr(obj)
+    except Exception:
+        result = object.__repr__(obj)
+    if not short or len(result) < _MAX_LENGTH:
+        return result
+    return result[:_MAX_LENGTH] + ' [truncated]...'
 
-def safe_repr(obj):
-    try:
-        return repr(obj)
-    except Exception:
-        return object.__repr__(obj)
 
 def strclass(cls):
     return "%s.%s" % (cls.__module__, cls.__name__)