diff options
Diffstat (limited to 'Lib/unittest.py')
-rw-r--r-- | Lib/unittest.py | 63 |
1 files changed, 35 insertions, 28 deletions
diff --git a/Lib/unittest.py b/Lib/unittest.py index cdccd8c..9c3024a 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -353,7 +353,7 @@ class TestCase(object): not have a method with the specified name. """ self._testMethodName = methodName - self._result = None + self._resultForDoCleanups = None try: testMethod = getattr(self, methodName) except AttributeError: @@ -457,7 +457,7 @@ class TestCase(object): if startTestRun is not None: startTestRun() - self._result = result + self._resultForDoCleanups = result result.startTest(self) if getattr(self.__class__, "__unittest_skip__", False): # If the whole class was skipped. @@ -511,7 +511,7 @@ class TestCase(object): def doCleanups(self): """Execute all cleanup functions. Normally called for you after tearDown.""" - result = self._result + result = self._resultForDoCleanups ok = True while self._cleanups: function, args, kwargs = self._cleanups.pop(-1) @@ -732,23 +732,32 @@ class TestCase(object): if seq1 == seq2: return + seq1_repr = repr(seq1) + seq2_repr = repr(seq2) + if len(seq1_repr) > 30: + seq1_repr = seq1_repr[:30] + '...' + if len(seq2_repr) > 30: + seq2_repr = seq2_repr[:30] + '...' + elements = (seq_type_name.capitalize(), seq1_repr, seq2_repr) + differing = '%ss differ: %s != %s\n' % elements + for i in range(min(len1, len2)): try: item1 = seq1[i] except (TypeError, IndexError, NotImplementedError): - differing = ('Unable to index element %d of first %s\n' % + differing += ('\nUnable to index element %d of first %s\n' % (i, seq_type_name)) break try: item2 = seq2[i] except (TypeError, IndexError, NotImplementedError): - differing = ('Unable to index element %d of second %s\n' % + differing += ('\nUnable to index element %d of second %s\n' % (i, seq_type_name)) break if item1 != item2: - differing = ('First differing element %d:\n%s\n%s\n' % + differing += ('\nFirst differing element %d:\n%s\n%s\n' % (i, item1, item2)) break else: @@ -756,28 +765,26 @@ class TestCase(object): type(seq1) != type(seq2)): # The sequences are the same, but have differing types. return - # A catch-all message for handling arbitrary user-defined - # sequences. - differing = '%ss differ:\n' % seq_type_name.capitalize() - if len1 > len2: - differing = ('First %s contains %d additional ' - 'elements.\n' % (seq_type_name, len1 - len2)) - try: - differing += ('First extra element %d:\n%s\n' % - (len2, seq1[len2])) - except (TypeError, IndexError, NotImplementedError): - differing += ('Unable to index element %d ' - 'of first %s\n' % (len2, seq_type_name)) - elif len1 < len2: - differing = ('Second %s contains %d additional ' - 'elements.\n' % (seq_type_name, len2 - len1)) - try: - differing += ('First extra element %d:\n%s\n' % - (len1, seq2[len1])) - except (TypeError, IndexError, NotImplementedError): - differing += ('Unable to index element %d ' - 'of second %s\n' % (len1, seq_type_name)) - standardMsg = differing + '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), + + if len1 > len2: + differing += ('\nFirst %s contains %d additional ' + 'elements.\n' % (seq_type_name, len1 - len2)) + try: + differing += ('First extra element %d:\n%s\n' % + (len2, seq1[len2])) + except (TypeError, IndexError, NotImplementedError): + differing += ('Unable to index element %d ' + 'of first %s\n' % (len2, seq_type_name)) + elif len1 < len2: + differing += ('\nSecond %s contains %d additional ' + 'elements.\n' % (seq_type_name, len2 - len1)) + try: + differing += ('First extra element %d:\n%s\n' % + (len1, seq2[len1])) + except (TypeError, IndexError, NotImplementedError): + differing += ('Unable to index element %d ' + 'of second %s\n' % (len1, seq_type_name)) + standardMsg = differing + '\n' + '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), pprint.pformat(seq2).splitlines())) msg = self._formatMessage(msg, standardMsg) self.fail(msg) |