Skip to content

Commit

Permalink
Reverse / fix meaning of "+/-" in error diffs
Browse files Browse the repository at this point in the history
The convention is "assert result is expected".  Pytest's error diffs now
reflect this. "-" means that sth. expected is missing in the result and
"+" means that there are unexpected extras in the result.

Fixes: #3333
  • Loading branch information
sscherfke committed Feb 10, 2020
1 parent 4038d6c commit d59adc6
Show file tree
Hide file tree
Showing 8 changed files with 360 additions and 79 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -246,6 +246,7 @@ Simon Gomizelj
Skylar Downes
Srinivas Reddy Thatiparthy
Stefan Farmbauer
Stefan Scherfke
Stefan Zimmermann
Stefano Taschini
Steffen Allner
Expand Down
1 change: 1 addition & 0 deletions changelog/6673.breaking.rst
@@ -0,0 +1 @@
Reversed / fix meaning of "+/-" in error diffs. "-" means that sth. expected is missing in the result and "+" means that there are unexpected extras in the result.
16 changes: 8 additions & 8 deletions doc/en/example/reportingdemo.rst
Expand Up @@ -81,8 +81,8 @@ Here is a nice run of several failures and how ``pytest`` presents things:
def test_eq_text(self):
> assert "spam" == "eggs"
E AssertionError: assert 'spam' == 'eggs'
E - spam
E + eggs
E - eggs
E + spam
failure_demo.py:45: AssertionError
_____________ TestSpecialisedExplanations.test_eq_similar_text _____________
Expand All @@ -92,9 +92,9 @@ Here is a nice run of several failures and how ``pytest`` presents things:
def test_eq_similar_text(self):
> assert "foo 1 bar" == "foo 2 bar"
E AssertionError: assert 'foo 1 bar' == 'foo 2 bar'
E - foo 1 bar
E - foo 2 bar
E ? ^
E + foo 2 bar
E + foo 1 bar
E ? ^
failure_demo.py:48: AssertionError
Expand All @@ -106,8 +106,8 @@ Here is a nice run of several failures and how ``pytest`` presents things:
> assert "foo\nspam\nbar" == "foo\neggs\nbar"
E AssertionError: assert 'foo\nspam\nbar' == 'foo\neggs\nbar'
E foo
E - spam
E + eggs
E - eggs
E + spam
E bar
failure_demo.py:51: AssertionError
Expand All @@ -122,9 +122,9 @@ Here is a nice run of several failures and how ``pytest`` presents things:
E AssertionError: assert '111111111111...2222222222222' == '111111111111...2222222222222'
E Skipping 90 identical leading characters in diff, use -v to show
E Skipping 91 identical trailing characters in diff, use -v to show
E - 1111111111a222222222
E - 1111111111b222222222
E ? ^
E + 1111111111b222222222
E + 1111111111a222222222
E ? ^
failure_demo.py:56: AssertionError
Expand Down
19 changes: 12 additions & 7 deletions src/_pytest/assertion/util.py
Expand Up @@ -225,9 +225,11 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]:
left = repr(str(left))
right = repr(str(right))
explanation += ["Strings contain only whitespace, escaping them using repr()"]
# "right" is the expected base against which we compare "left",
# see https://github.com/pytest-dev/pytest/issues/3333
explanation += [
line.strip("\n")
for line in ndiff(left.splitlines(keepends), right.splitlines(keepends))
for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
]
return explanation

Expand All @@ -238,8 +240,8 @@ def _compare_eq_verbose(left: Any, right: Any) -> List[str]:
right_lines = repr(right).splitlines(keepends)

explanation = [] # type: List[str]
explanation += ["-" + line for line in left_lines]
explanation += ["+" + line for line in right_lines]
explanation += ["+" + line for line in left_lines]
explanation += ["-" + line for line in right_lines]

return explanation

Expand Down Expand Up @@ -279,8 +281,10 @@ def _compare_eq_iterable(
_surrounding_parens_on_own_lines(right_formatting)

explanation = ["Full diff:"]
# "right" is the expected base against which we compare "left",
# see https://github.com/pytest-dev/pytest/issues/3333
explanation.extend(
line.rstrip() for line in difflib.ndiff(left_formatting, right_formatting)
line.rstrip() for line in difflib.ndiff(right_formatting, left_formatting)
)
return explanation

Expand Down Expand Up @@ -315,8 +319,9 @@ def _compare_eq_sequence(
break

if comparing_bytes:
# when comparing bytes, it doesn't help to show the "sides contain one or more items"
# longer explanation, so skip it
# when comparing bytes, it doesn't help to show the "sides contain one or more
# items" longer explanation, so skip it

return explanation

len_diff = len_left - len_right
Expand Down Expand Up @@ -443,7 +448,7 @@ def _notin_text(term: str, text: str, verbose: int = 0) -> List[str]:
head = text[:index]
tail = text[index + len(term) :]
correct_text = head + tail
diff = _diff_text(correct_text, text, verbose)
diff = _diff_text(text, correct_text, verbose)
newdiff = ["%s is contained here:" % saferepr(term, maxsize=42)]
for line in diff:
if line.startswith("Skipping"):
Expand Down
4 changes: 2 additions & 2 deletions testing/acceptance_test.py
Expand Up @@ -1277,8 +1277,8 @@ def test():
" def check():",
"> assert 1 == 2",
"E assert 1 == 2",
"E -1",
"E +2",
"E +1",
"E -2",
"",
"pdb.py:2: AssertionError",
"*= 1 failed in *",
Expand Down

0 comments on commit d59adc6

Please sign in to comment.