Commit e37d75fc authored by Michael Foord's avatar Michael Foord

Removed the new max_diff argument to assertSequenceEqual. All...

Removed the new max_diff argument to assertSequenceEqual. All unittest.TestCase assert methods that use difflib to produce failure messages now truncate overly long messages. New class attribute unittest.TestCase.maxDiff to configure this if necessary. Issue 8351.
parent 23f0d6b5
...@@ -13,7 +13,6 @@ from .util import ( ...@@ -13,7 +13,6 @@ from .util import (
) )
__unittest = True __unittest = True
TRUNCATED_DIFF = '\n[diff truncated...]'
class SkipTest(Exception): class SkipTest(Exception):
""" """
...@@ -157,6 +156,11 @@ class TestCase(object): ...@@ -157,6 +156,11 @@ class TestCase(object):
longMessage = False longMessage = False
# This attribute sets the maximum length of a diff in failure messsages
# by assert methods using difflib. It is looked up as an instance attribute
# so can be configured by individual tests if required.
maxDiff = 80*8
# Attribute used by TestSuite for classSetUp # Attribute used by TestSuite for classSetUp
_classSetupFailed = False _classSetupFailed = False
...@@ -589,8 +593,7 @@ class TestCase(object): ...@@ -589,8 +593,7 @@ class TestCase(object):
failUnlessRaises = _deprecate(assertRaises) failUnlessRaises = _deprecate(assertRaises)
failIf = _deprecate(assertFalse) 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). """An equality assertion for ordered sequences (like lists and tuples).
For the purposes of this function, a valid ordered sequence type is one For the purposes of this function, a valid ordered sequence type is one
...@@ -603,7 +606,6 @@ class TestCase(object): ...@@ -603,7 +606,6 @@ class TestCase(object):
datatype should be enforced. datatype should be enforced.
msg: Optional message to use on failure instead of a list of msg: Optional message to use on failure instead of a list of
differences. differences.
max_diff: Maximum size off the diff, larger diffs are not shown
""" """
if seq_type is not None: if seq_type is not None:
seq_type_name = seq_type.__name__ seq_type_name = seq_type.__name__
...@@ -690,14 +692,15 @@ class TestCase(object): ...@@ -690,14 +692,15 @@ class TestCase(object):
diffMsg = '\n' + '\n'.join( diffMsg = '\n' + '\n'.join(
difflib.ndiff(pprint.pformat(seq1).splitlines(), difflib.ndiff(pprint.pformat(seq1).splitlines(),
pprint.pformat(seq2).splitlines())) pprint.pformat(seq2).splitlines()))
standardMsg = self._truncateMessage(standardMsg, diffMsg, max_diff) standardMsg = self._truncateMessage(standardMsg, diffMsg)
msg = self._formatMessage(msg, standardMsg) msg = self._formatMessage(msg, standardMsg)
self.fail(msg) self.fail(msg)
def _truncateMessage(self, message, diff, max_diff): def _truncateMessage(self, message, diff):
max_diff = self.maxDiff
if max_diff is None or len(diff) <= max_diff: if max_diff is None or len(diff) <= max_diff:
return message + diff return message + diff
return message + diff[:max_diff] + TRUNCATED_DIFF return message
def assertListEqual(self, list1, list2, msg=None): def assertListEqual(self, list1, list2, msg=None):
"""A list-specific equality assertion. """A list-specific equality assertion.
...@@ -797,9 +800,10 @@ class TestCase(object): ...@@ -797,9 +800,10 @@ class TestCase(object):
self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary') self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary')
if d1 != d2: if d1 != d2:
standardMsg = ('\n' + '\n'.join(difflib.ndiff( diff = ('\n' + '\n'.join(difflib.ndiff(
pprint.pformat(d1).splitlines(), pprint.pformat(d1).splitlines(),
pprint.pformat(d2).splitlines()))) pprint.pformat(d2).splitlines())))
standardMsg = self._truncateMessage('', diff)
self.fail(self._formatMessage(msg, standardMsg)) self.fail(self._formatMessage(msg, standardMsg))
def assertDictContainsSubset(self, expected, actual, msg=None): def assertDictContainsSubset(self, expected, actual, msg=None):
...@@ -882,8 +886,9 @@ class TestCase(object): ...@@ -882,8 +886,9 @@ class TestCase(object):
'Second argument is not a string')) 'Second argument is not a string'))
if first != second: if first != second:
standardMsg = '\n' + ''.join(difflib.ndiff(first.splitlines(True), diff = '\n' + ''.join(difflib.ndiff(first.splitlines(True),
second.splitlines(True))) second.splitlines(True)))
standardMsg = self._truncateMessage('', diff)
self.fail(self._formatMessage(msg, standardMsg)) self.fail(self._formatMessage(msg, standardMsg))
def assertLess(self, a, b, msg=None): def assertLess(self, a, b, msg=None):
......
...@@ -591,20 +591,37 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): ...@@ -591,20 +591,37 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
self.assertRaises(self.failureException, self.assertDictEqual, 1, 1) self.assertRaises(self.failureException, self.assertDictEqual, 1, 1)
def testAssertSequenceEqualMaxDiff(self): def testAssertSequenceEqualMaxDiff(self):
self.assertEqual(self.maxDiff, 80*8)
seq1 = 'a' + 'x' * 80**2 seq1 = 'a' + 'x' * 80**2
seq2 = 'b' + 'x' * 80**2 seq2 = 'b' + 'x' * 80**2
diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(),
pprint.pformat(seq2).splitlines())) pprint.pformat(seq2).splitlines()))
self.maxDiff = len(diff)//2
try: try:
self.assertSequenceEqual(seq1, seq2, max_diff=len(diff)/2) self.assertSequenceEqual(seq1, seq2)
except AssertionError as e: except self.failureException as e:
msg = e.args[0] msg = e.args[0]
else:
self.fail('assertSequenceEqual did not fail.')
self.assertTrue(len(msg) < len(diff)) self.assertTrue(len(msg) < len(diff))
self.maxDiff = len(diff) * 2
try:
self.assertSequenceEqual(seq1, seq2)
except self.failureException as e:
msg = e.args[0]
else:
self.fail('assertSequenceEqual did not fail.')
self.assertTrue(len(msg) > len(diff))
self.maxDiff = None
try: try:
self.assertSequenceEqual(seq1, seq2, max_diff=len(diff)*2) self.assertSequenceEqual(seq1, seq2)
except AssertionError as e: except self.failureException as e:
msg = e.args[0] msg = e.args[0]
else:
self.fail('assertSequenceEqual did not fail.')
self.assertTrue(len(msg) > len(diff)) self.assertTrue(len(msg) > len(diff))
def testAssertItemsEqual(self): def testAssertItemsEqual(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment