Skip to content

Commit e6ea9ed

Browse files
authored
Merge pull request #6673 from sscherfke/features
Reverse / fix meaning of "+/-" in error diffs
2 parents b7ad4c2 + d59adc6 commit e6ea9ed

File tree

8 files changed

+360
-79
lines changed

8 files changed

+360
-79
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Simon Gomizelj
246246
Skylar Downes
247247
Srinivas Reddy Thatiparthy
248248
Stefan Farmbauer
249+
Stefan Scherfke
249250
Stefan Zimmermann
250251
Stefano Taschini
251252
Steffen Allner

changelog/6673.breaking.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

doc/en/example/reportingdemo.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ Here is a nice run of several failures and how ``pytest`` presents things:
8181
def test_eq_text(self):
8282
> assert "spam" == "eggs"
8383
E AssertionError: assert 'spam' == 'eggs'
84-
E - spam
85-
E + eggs
84+
E - eggs
85+
E + spam
8686
8787
failure_demo.py:45: AssertionError
8888
_____________ TestSpecialisedExplanations.test_eq_similar_text _____________
@@ -92,9 +92,9 @@ Here is a nice run of several failures and how ``pytest`` presents things:
9292
def test_eq_similar_text(self):
9393
> assert "foo 1 bar" == "foo 2 bar"
9494
E AssertionError: assert 'foo 1 bar' == 'foo 2 bar'
95-
E - foo 1 bar
95+
E - foo 2 bar
9696
E ? ^
97-
E + foo 2 bar
97+
E + foo 1 bar
9898
E ? ^
9999
100100
failure_demo.py:48: AssertionError
@@ -106,8 +106,8 @@ Here is a nice run of several failures and how ``pytest`` presents things:
106106
> assert "foo\nspam\nbar" == "foo\neggs\nbar"
107107
E AssertionError: assert 'foo\nspam\nbar' == 'foo\neggs\nbar'
108108
E foo
109-
E - spam
110-
E + eggs
109+
E - eggs
110+
E + spam
111111
E bar
112112
113113
failure_demo.py:51: AssertionError
@@ -122,9 +122,9 @@ Here is a nice run of several failures and how ``pytest`` presents things:
122122
E AssertionError: assert '111111111111...2222222222222' == '111111111111...2222222222222'
123123
E Skipping 90 identical leading characters in diff, use -v to show
124124
E Skipping 91 identical trailing characters in diff, use -v to show
125-
E - 1111111111a222222222
125+
E - 1111111111b222222222
126126
E ? ^
127-
E + 1111111111b222222222
127+
E + 1111111111a222222222
128128
E ? ^
129129
130130
failure_demo.py:56: AssertionError

src/_pytest/assertion/util.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]:
225225
left = repr(str(left))
226226
right = repr(str(right))
227227
explanation += ["Strings contain only whitespace, escaping them using repr()"]
228+
# "right" is the expected base against which we compare "left",
229+
# see https://github.com/pytest-dev/pytest/issues/3333
228230
explanation += [
229231
line.strip("\n")
230-
for line in ndiff(left.splitlines(keepends), right.splitlines(keepends))
232+
for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
231233
]
232234
return explanation
233235

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

240242
explanation = [] # type: List[str]
241-
explanation += ["-" + line for line in left_lines]
242-
explanation += ["+" + line for line in right_lines]
243+
explanation += ["+" + line for line in left_lines]
244+
explanation += ["-" + line for line in right_lines]
243245

244246
return explanation
245247

@@ -279,8 +281,10 @@ def _compare_eq_iterable(
279281
_surrounding_parens_on_own_lines(right_formatting)
280282

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

@@ -315,8 +319,9 @@ def _compare_eq_sequence(
315319
break
316320

317321
if comparing_bytes:
318-
# when comparing bytes, it doesn't help to show the "sides contain one or more items"
319-
# longer explanation, so skip it
322+
# when comparing bytes, it doesn't help to show the "sides contain one or more
323+
# items" longer explanation, so skip it
324+
320325
return explanation
321326

322327
len_diff = len_left - len_right
@@ -443,7 +448,7 @@ def _notin_text(term: str, text: str, verbose: int = 0) -> List[str]:
443448
head = text[:index]
444449
tail = text[index + len(term) :]
445450
correct_text = head + tail
446-
diff = _diff_text(correct_text, text, verbose)
451+
diff = _diff_text(text, correct_text, verbose)
447452
newdiff = ["%s is contained here:" % saferepr(term, maxsize=42)]
448453
for line in diff:
449454
if line.startswith("Skipping"):

testing/acceptance_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,8 @@ def test():
12791279
" def check():",
12801280
"> assert 1 == 2",
12811281
"E assert 1 == 2",
1282-
"E -1",
1283-
"E +2",
1282+
"E +1",
1283+
"E -2",
12841284
"",
12851285
"pdb.py:2: AssertionError",
12861286
"*= 1 failed in *",

0 commit comments

Comments
 (0)