[Python-checkins] r81748 - in python/branches/py3k: Lib/unittest/case.py Lib/unittest/util.py

michael.foord python-checkins at python.org
Sat Jun 5 15:14:43 CEST 2010


Author: michael.foord
Date: Sat Jun  5 15:14:43 2010
New Revision: 81748

Log:
Merged revisions 81747 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r81747 | michael.foord | 2010-06-05 13:58:39 +0100 (Sat, 05 Jun 2010) | 1 line
  
  unittest.TestCase.assertDictEqual and assertMultilineEqual provide better default failure messages in the event of long diffs.
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Lib/unittest/case.py
   python/branches/py3k/Lib/unittest/util.py

Modified: python/branches/py3k/Lib/unittest/case.py
==============================================================================
--- python/branches/py3k/Lib/unittest/case.py	(original)
+++ python/branches/py3k/Lib/unittest/case.py	Sat Jun  5 15:14:43 2010
@@ -811,10 +811,11 @@
         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):
@@ -931,9 +932,10 @@
                 '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):

Modified: python/branches/py3k/Lib/unittest/util.py
==============================================================================
--- python/branches/py3k/Lib/unittest/util.py	(original)
+++ python/branches/py3k/Lib/unittest/util.py	Sat Jun  5 15:14:43 2010
@@ -2,12 +2,16 @@
 
 __unittest = True
 
-
-def safe_repr(obj):
+_MAX_LENGTH = 80
+def safe_repr(obj, short=False):
     try:
-        return repr(obj)
+        result = repr(obj)
     except Exception:
-        return object.__repr__(obj)
+        result = object.__repr__(obj)
+    if not short or len(result) < _MAX_LENGTH:
+        return result
+    return result[:_MAX_LENGTH] + ' [truncated]...'
+
 
 def strclass(cls):
     return "%s.%s" % (cls.__module__, cls.__name__)


More information about the Python-checkins mailing list