summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-06-05 12:58:39 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-06-05 12:58:39 (GMT)
commit674648e3f2020e30bf73fed7ac77625d6f159de2 (patch)
tree65278bf23510fc853a2c1b4e230e42b7183925f5 /Lib/unittest
parent77acee95677152af7417ad47148cf24feb4c947c (diff)
downloadcpython-674648e3f2020e30bf73fed7ac77625d6f159de2.zip
cpython-674648e3f2020e30bf73fed7ac77625d6f159de2.tar.gz
cpython-674648e3f2020e30bf73fed7ac77625d6f159de2.tar.bz2
unittest.TestCase.assertDictEqual and assertMultilineEqual provide better default failure messages in the event of long diffs.
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/case.py6
-rw-r--r--Lib/unittest/util.py12
2 files changed, 12 insertions, 6 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index b984bb9..ff6b8b3 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -800,10 +800,11 @@ class TestCase(object):
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):
@@ -886,9 +887,10 @@ class TestCase(object):
'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):
diff --git a/Lib/unittest/util.py b/Lib/unittest/util.py
index 7d90ddf..d201657 100644
--- a/Lib/unittest/util.py
+++ b/Lib/unittest/util.py
@@ -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__)