diff options
-rw-r--r-- | Lib/unittest/case.py | 13 | ||||
-rw-r--r-- | Lib/unittest/test/test_case.py | 19 |
2 files changed, 29 insertions, 3 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index a072cf1..4a13f0a 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -12,7 +12,7 @@ from .util import (strclass, safe_repr, sorted_list_difference, unorderable_list_difference) __unittest = True - +TRUNCATED_DIFF = '\n[diff truncated...]' class SkipTest(Exception): """ @@ -599,7 +599,8 @@ class TestCase(object): failUnlessRaises = _deprecate(assertRaises) failIf = _deprecate(assertFalse) - def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None): + def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None, + max_diff=80*8): """An equality assertion for ordered sequences (like lists and tuples). For the purposes of this function, a valid ordered sequence type is one @@ -612,6 +613,7 @@ class TestCase(object): datatype should be enforced. msg: Optional message to use on failure instead of a list of differences. + max_diff: Maximum size off the diff, larger diffs are not shown """ if seq_type != None: seq_type_name = seq_type.__name__ @@ -694,9 +696,14 @@ class TestCase(object): except (TypeError, IndexError, NotImplementedError): differing += ('Unable to index element %d ' 'of second %s\n' % (len1, seq_type_name)) - standardMsg = differing + '\n' + '\n'.join( + standardMsg = differing + diffMsg = '\n' + '\n'.join( difflib.ndiff(pprint.pformat(seq1).splitlines(), pprint.pformat(seq2).splitlines())) + if max_diff is None or len(diffMsg) <= max_diff: + standardMsg += diffMsg + else: + standardMsg += diffMsg[:max_diff] + TRUNCATED_DIFF msg = self._formatMessage(msg, standardMsg) self.fail(msg) diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index 08c4a7b..21dd857 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -1,3 +1,5 @@ +import difflib +import pprint import re import sys @@ -589,6 +591,23 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): self.assertRaises(self.failureException, self.assertDictEqual, [], d) self.assertRaises(self.failureException, self.assertDictEqual, 1, 1) + def testAssertSequenceEqualMaxDiff(self): + seq1 = 'a' + 'x' * 80**2 + seq2 = 'b' + 'x' * 80**2 + diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), + pprint.pformat(seq2).splitlines())) + try: + self.assertSequenceEqual(seq1, seq2, max_diff=len(diff)/2) + except AssertionError as e: + msg = e.args[0] + self.assertTrue(len(msg) < len(diff)) + + try: + self.assertSequenceEqual(seq1, seq2, max_diff=len(diff)*2) + except AssertionError as e: + msg = e.args[0] + self.assertTrue(len(msg) > len(diff)) + def testAssertItemsEqual(self): a = object() self.assertItemsEqual([1, 2, 3], [3, 2, 1]) |