[Python-checkins] r81728 - in python/trunk/Lib/unittest: case.py test/test_case.py

michael.foord python-checkins at python.org
Sat Jun 5 13:23:51 CEST 2010


Author: michael.foord
Date: Sat Jun  5 13:23:51 2010
New Revision: 81728

Log:
Issue 8351. Suppress large diffs in unittest.TestCase.assertSequenceEqual.

Modified:
   python/trunk/Lib/unittest/case.py
   python/trunk/Lib/unittest/test/test_case.py

Modified: python/trunk/Lib/unittest/case.py
==============================================================================
--- python/trunk/Lib/unittest/case.py	(original)
+++ python/trunk/Lib/unittest/case.py	Sat Jun  5 13:23:51 2010
@@ -13,7 +13,7 @@
 )
 
 __unittest = True
-
+TRUNCATED_DIFF = '\n[diff truncated...]'
 
 class SkipTest(Exception):
     """
@@ -589,7 +589,8 @@
     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
@@ -602,6 +603,7 @@
                     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 is not None:
             seq_type_name = seq_type.__name__
@@ -684,9 +686,14 @@
                 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)
 

Modified: python/trunk/Lib/unittest/test/test_case.py
==============================================================================
--- python/trunk/Lib/unittest/test/test_case.py	(original)
+++ python/trunk/Lib/unittest/test/test_case.py	Sat Jun  5 13:23:51 2010
@@ -1,3 +1,5 @@
+import difflib
+import pprint
 import re
 import sys
 
@@ -588,6 +590,23 @@
         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])


More information about the Python-checkins mailing list