Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated diff to show \t in string comparison assertions #10961

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Eduardo Schettino
Eli Boyarski
Elizaveta Shashkova
Éloi Rivard
Emmeline Wetzel
Endre Galaczi
Eric Hunsberger
Eric Liu
Expand Down Expand Up @@ -220,6 +221,7 @@ Loic Esteve
Lukas Bednar
Luke Murphy
Maciek Fijalkowski
Madeline Thai-Tang
Maho
Maik Figura
Mandeep Bhutani
Expand Down
1 change: 1 addition & 0 deletions changelog/10704.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed the diff explanation for string comparison assertions when there is a tab in the string to show ``\t`` instead of four spaces.
4 changes: 4 additions & 0 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]:
line.strip("\n")
for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
]
for i in range(len(explanation)):
if "? " not in explanation[i]: # dont replace diff message tab
explanation[i] = explanation[i].replace("+ ", "+ \\t")
explanation[i] = explanation[i].replace(" ", "\\t")
return explanation


Expand Down
17 changes: 17 additions & 0 deletions testing/test_error_diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ def test_this():
""",
id="Compare attrs classes",
),
pytest.param(
"""
def test_this():
result = '''spam bacon
eggs love'''
desired = "spam bacon eggs love"
assert result == desired
""",
"""
> assert result == desired
E AssertionError: assert 'spam bacon\\n eggs love' == 'spam bacon eggs love'
E - spam bacon eggs love
E + spam\\tbacon
E + \\teggs love
Comment on lines +268 to +278
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong to me - there are no tab characters in the original strings, so we're actually lying about the contents by replacing four spaces!

I think we need a different approach, perhaps showing the repr of the explanation lines in question if they differ in their trailing whitespace; or maybe only showing that for the trailing whitespace in question. Check out what the short summary does I guess?

""",
id="Test tab repr in diff",
),
]


Expand Down