This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: assertEqual doesn't display newline differences quite well
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: ezio.melotti, gregory.p.smith, iritkatriel, michael.foord, mouad, pitrou, terry.reedy
Priority: low Keywords: patch

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 "<stdin>", line 1, in <module>
  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:17adminsetgithub: 56247
2021-12-09 21:45:16iritkatrielsetnosy: + iritkatriel

messages: + msg408160
versions: + Python 3.11, - Python 3.3
2011-06-26 15:25:17mouadsetfiles: + issue-12038.patch

nosy: + mouad
messages: + msg139190

keywords: + patch
2011-05-16 11:52:38michael.foordsetassignee: michael.foord
messages: + msg136079
2011-05-15 04:39:35gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg136007
2011-05-14 05:22:14terry.reedysetnosy: + terry.reedy
messages: + msg135953
2011-05-09 21:40:50michael.foordsetmessages: + msg135657
2011-05-09 16:36:50pitroucreate