Issue 12038: assertEqual doesn't display newline differences quite well (original) (raw)

Created on 2011-05-09 16:36 by pitrou, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
issue-12038.patch mouad,2011-06-26 15:25 Print a more human readable assert message when two strings don't match. review
Messages (7)
msg135608 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-05-09 16:36
>>> tc.assertEqual("x\n" * 40 + "\n", "x\n" * 40 + "\r\n") Traceback (most recent call last): File "", line 1, in File "/home/antoine/cpython/default/Lib/unittest/case.py", line 662, in assertEqual assertion_func(first, second, msg=msg) File "/home/antoine/cpython/default/Lib/unittest/case.py", line 1041, in assertMultiLineEqual self.fail(self._formatMessage(msg, standardMsg)) File "/home/antoine/cpython/default/Lib/unittest/case.py", line 531, in fail raise self.failureException(msg) AssertionError: 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx [truncated]... != 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx [truncated]... x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x - + It isn't obvious when looking at this printout that the last newline differed.
msg135657 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2011-05-09 21:40
Yup, I agree that's pretty sucky. Any suggestions for a fix?
msg135953 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-05-14 05:22
A possible fix is to condense the output by omitting stuff in the center rather than as the end: "x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx...x\nx\nx\nx\nx\nx\nx\nx\nx\nx\n" "x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx...x\nx\nx\nx\nx\nx\nx\nx\nx\nx\r\n" would be clear.
msg136007 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2011-05-15 04:39
We should ensure that the first differing character in the string is always included in what is displayed as a diff. if we're going to shorten a string we should elide something that matches.
msg136079 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2011-05-16 11:52
Yep, ensuring the truncated repr includes at least the *first* difference sounds like the right approach.
msg139190 - (view) Author: mouad (mouad) * Date: 2011-06-26 15:25
I took on consideration the two suggestion that was in the issue and create this patch that basically show only until the last character that differ and truncate the rest. I just included one test case, other test case if they should be included (and if my solution got accepted :)), should be more *complex* and check using Regex the correctness of the assertion message, but here is the output that we can see when applying the patch: >>> tc.assertEqual("x\n" * 40 + "\n", "x\n" * 40 + "\r\n") 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n...nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n\n [truncated]... != 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n...\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n\r [truncated]... x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x - + >>> tc.assertEqual('aaaa', 'abbb') AssertionError: 'aa [truncated]... != 'ab [truncated]... - aaaa + abbb >>> tc.assertEqual('aaaa', 'bbbb') AssertionError: 'a [truncated]... != 'b [truncated]... - aaaa + bbbb >>> tc.assertEqual("x\n" * 80 + "\n", "x\n" * 80 + "\r") AssertionError: 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n...nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n\n [truncated]... != 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n...nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n\r [truncated]...
msg408160 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-09 21:45
Reproduced on 3.11.
History
Date User Action Args
2022-04-11 14:57:17 admin set github: 56247
2021-12-09 21:45:16 iritkatriel set nosy: + iritkatrielmessages: + versions: + Python 3.11, - Python 3.3
2011-06-26 15:25:17 mouad set files: + issue-12038.patchnosy: + mouadmessages: + keywords: + patch
2011-05-16 11:52:38 michael.foord set assignee: michael.foordmessages: +
2011-05-15 04:39:35 gregory.p.smith set nosy: + gregory.p.smithmessages: +
2011-05-14 05:22:14 terry.reedy set nosy: + terry.reedymessages: +
2011-05-09 21:40:50 michael.foord set messages: +
2011-05-09 16:36:50 pitrou create