Skip to content

Commit fac8f28

Browse files
authoredFeb 15, 2022
Fix diff output for data types where -v would show less information (#9661)
Close #5192
1 parent afe41e5 commit fac8f28

File tree

5 files changed

+19
-54
lines changed

5 files changed

+19
-54
lines changed
 

‎changelog/5192.improvement.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed test output for some data types where ``-v`` would show less information.
2+
3+
Also, when showing diffs for sequences, ``-q`` would produce full diffs instead of the expected diff.

‎src/_pytest/assertion/util.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ def _compare_eq_any(left: Any, right: Any, verbose: int = 0) -> List[str]:
223223
explanation = _compare_eq_set(left, right, verbose)
224224
elif isdict(left) and isdict(right):
225225
explanation = _compare_eq_dict(left, right, verbose)
226-
elif verbose > 0:
227-
explanation = _compare_eq_verbose(left, right)
228226

229227
if isiterable(left) and isiterable(right):
230228
expl = _compare_eq_iterable(left, right, verbose)
@@ -281,18 +279,6 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]:
281279
return explanation
282280

283281

284-
def _compare_eq_verbose(left: Any, right: Any) -> List[str]:
285-
keepends = True
286-
left_lines = repr(left).splitlines(keepends)
287-
right_lines = repr(right).splitlines(keepends)
288-
289-
explanation: List[str] = []
290-
explanation += ["+" + line for line in left_lines]
291-
explanation += ["-" + line for line in right_lines]
292-
293-
return explanation
294-
295-
296282
def _surrounding_parens_on_own_lines(lines: List[str]) -> None:
297283
"""Move opening/closing parenthesis/bracket to own lines."""
298284
opening = lines[0][:1]
@@ -308,8 +294,8 @@ def _surrounding_parens_on_own_lines(lines: List[str]) -> None:
308294
def _compare_eq_iterable(
309295
left: Iterable[Any], right: Iterable[Any], verbose: int = 0
310296
) -> List[str]:
311-
if not verbose and not running_on_ci():
312-
return ["Use -v to get the full diff"]
297+
if verbose <= 0 and not running_on_ci():
298+
return ["Use -v to get more diff"]
313299
# dynamic import to speedup pytest
314300
import difflib
315301

‎testing/acceptance_test.py

-2
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,6 @@ def test():
12381238
" def check():",
12391239
"> assert 1 == 2",
12401240
"E assert 1 == 2",
1241-
"E +1",
1242-
"E -2",
12431241
"",
12441242
"pdb.py:2: AssertionError",
12451243
"*= 1 failed in *",

‎testing/test_assertion.py

+14-34
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_dummy_failure(pytester): # how meta!
8383
"E assert {'failed': 1,... 'skipped': 0} == {'failed': 0,... 'skipped': 0}",
8484
"E Omitting 1 identical items, use -vv to show",
8585
"E Differing items:",
86-
"E Use -v to get the full diff",
86+
"E Use -v to get more diff",
8787
]
8888
)
8989
# XXX: unstable output.
@@ -376,7 +376,7 @@ def test_bytes_diff_normal(self) -> None:
376376
assert diff == [
377377
"b'spam' == b'eggs'",
378378
"At index 0 diff: b's' != b'e'",
379-
"Use -v to get the full diff",
379+
"Use -v to get more diff",
380380
]
381381

382382
def test_bytes_diff_verbose(self) -> None:
@@ -444,11 +444,19 @@ def test_iterable_full_diff(self, left, right, expected) -> None:
444444
"""
445445
expl = callequal(left, right, verbose=0)
446446
assert expl is not None
447-
assert expl[-1] == "Use -v to get the full diff"
447+
assert expl[-1] == "Use -v to get more diff"
448448
verbose_expl = callequal(left, right, verbose=1)
449449
assert verbose_expl is not None
450450
assert "\n".join(verbose_expl).endswith(textwrap.dedent(expected).strip())
451451

452+
def test_iterable_quiet(self) -> None:
453+
expl = callequal([1, 2], [10, 2], verbose=-1)
454+
assert expl == [
455+
"[1, 2] == [10, 2]",
456+
"At index 0 diff: 1 != 10",
457+
"Use -v to get more diff",
458+
]
459+
452460
def test_iterable_full_diff_ci(
453461
self, monkeypatch: MonkeyPatch, pytester: Pytester
454462
) -> None:
@@ -466,7 +474,7 @@ def test_full_diff():
466474

467475
monkeypatch.delenv("CI", raising=False)
468476
result = pytester.runpytest()
469-
result.stdout.fnmatch_lines(["E Use -v to get the full diff"])
477+
result.stdout.fnmatch_lines(["E Use -v to get more diff"])
470478

471479
def test_list_different_lengths(self) -> None:
472480
expl = callequal([0, 1], [0, 1, 2])
@@ -699,32 +707,6 @@ def test_list_tuples(self) -> None:
699707
assert expl is not None
700708
assert len(expl) > 1
701709

702-
def test_repr_verbose(self) -> None:
703-
class Nums:
704-
def __init__(self, nums):
705-
self.nums = nums
706-
707-
def __repr__(self):
708-
return str(self.nums)
709-
710-
list_x = list(range(5000))
711-
list_y = list(range(5000))
712-
list_y[len(list_y) // 2] = 3
713-
nums_x = Nums(list_x)
714-
nums_y = Nums(list_y)
715-
716-
assert callequal(nums_x, nums_y) is None
717-
718-
expl = callequal(nums_x, nums_y, verbose=1)
719-
assert expl is not None
720-
assert "+" + repr(nums_x) in expl
721-
assert "-" + repr(nums_y) in expl
722-
723-
expl = callequal(nums_x, nums_y, verbose=2)
724-
assert expl is not None
725-
assert "+" + repr(nums_x) in expl
726-
assert "-" + repr(nums_y) in expl
727-
728710
def test_list_bad_repr(self) -> None:
729711
class A:
730712
def __repr__(self):
@@ -851,8 +833,6 @@ def test_recursive_dataclasses_verbose(self, pytester: Pytester) -> None:
851833
"E ",
852834
"E Drill down into differing attribute a:",
853835
"E a: 10 != 20",
854-
"E +10",
855-
"E -20",
856836
"E ",
857837
"E Drill down into differing attribute b:",
858838
"E b: 'ten' != 'xxx'",
@@ -1059,7 +1039,7 @@ def test_namedtuple(self) -> None:
10591039
" b: 'b' != 'c'",
10601040
" - c",
10611041
" + b",
1062-
"Use -v to get the full diff",
1042+
"Use -v to get more diff",
10631043
]
10641044

10651045
def test_comparing_two_different_namedtuple(self) -> None:
@@ -1074,7 +1054,7 @@ def test_comparing_two_different_namedtuple(self) -> None:
10741054
assert lines == [
10751055
"NT1(a=1, b='b') == NT2(a=2, b='b')",
10761056
"At index 0 diff: 1 != 2",
1077-
"Use -v to get the full diff",
1057+
"Use -v to get more diff",
10781058
]
10791059

10801060

‎testing/test_error_diffs.py

-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ def test_this():
231231
E ['a']
232232
E Drill down into differing attribute a:
233233
E a: 1 != 2
234-
E +1
235-
E -2
236234
""",
237235
id="Compare data classes",
238236
),

0 commit comments

Comments
 (0)
Please sign in to comment.