diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -11,7 +11,7 @@ import contextlib from . import result -from .util import (strclass, safe_repr, _count_diff_all_purpose, +from .util import (strclass, safe_repr, truncate_str, _count_diff_all_purpose, _count_diff_hashable, _common_shorten_repr) __unittest = True @@ -770,7 +770,15 @@ def _baseAssertEqual(self, first, second, msg=None): """The default assertEqual implementation, not type specific.""" if not first == second: - standardMsg = '%s != %s' % _common_shorten_repr(first, second) + firstrepr, secondrepr = _common_shorten_repr(first, second) + # check that standardMsg will be shorter than maxDiff + maxDiff = self.maxDiff + if (maxDiff is not None and + (len(firstrepr) + len(secondrepr) + 8) > maxDiff): + standardMsg = '%s != %s' % (truncate_str(firstrepr, maxDiff), + truncate_str(secondrepr, maxDiff)) + else: + standardMsg = '%s != %s' % (firstrepr, secondrepr) msg = self._formatMessage(msg, standardMsg) raise self.failureException(msg) diff --git a/Lib/unittest/util.py b/Lib/unittest/util.py --- a/Lib/unittest/util.py +++ b/Lib/unittest/util.py @@ -49,7 +49,10 @@ result = object.__repr__(obj) if not short or len(result) < _MAX_LENGTH: return result - return result[:_MAX_LENGTH] + ' [truncated]...' + return truncate_str(result) + +def truncate_str(obj, maxlen=_MAX_LENGTH): + return obj[:maxlen] + ' [truncated]...' def strclass(cls): return "%s.%s" % (cls.__module__, cls.__name__)