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

Fix diff output for data types where -v would show less information #9661

Merged
merged 1 commit into from Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/5192.improvement.rst
@@ -0,0 +1,3 @@
Fixed test output for some data types where ``-v`` would show less information.
Copy link
Member Author

Choose a reason for hiding this comment

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

Marked this as an "improvement", but arguably this can be considered a bugfix.


Also, when showing diffs for sequences, ``-q`` would produce full diffs instead of the expected diff.
18 changes: 2 additions & 16 deletions src/_pytest/assertion/util.py
Expand Up @@ -223,8 +223,6 @@ def _compare_eq_any(left: Any, right: Any, verbose: int = 0) -> List[str]:
explanation = _compare_eq_set(left, right, verbose)
elif isdict(left) and isdict(right):
explanation = _compare_eq_dict(left, right, verbose)
elif verbose > 0:
explanation = _compare_eq_verbose(left, right)

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


def _compare_eq_verbose(left: Any, right: Any) -> List[str]:
keepends = True
left_lines = repr(left).splitlines(keepends)
right_lines = repr(right).splitlines(keepends)

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

return explanation


def _surrounding_parens_on_own_lines(lines: List[str]) -> None:
"""Move opening/closing parenthesis/bracket to own lines."""
opening = lines[0][:1]
Expand All @@ -308,8 +294,8 @@ def _surrounding_parens_on_own_lines(lines: List[str]) -> None:
def _compare_eq_iterable(
left: Iterable[Any], right: Iterable[Any], verbose: int = 0
) -> List[str]:
if not verbose and not running_on_ci():
return ["Use -v to get the full diff"]
if verbose <= 0 and not running_on_ci():
return ["Use -v to get more diff"]
Copy link
Member Author

Choose a reason for hiding this comment

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

The "full diff" here is misleading, because the users would use -v, and in the next run we show Use -vv to get the full diff again if the output is too large.

# dynamic import to speedup pytest
import difflib

Expand Down
2 changes: 0 additions & 2 deletions testing/acceptance_test.py
Expand Up @@ -1238,8 +1238,6 @@ def test():
" def check():",
"> assert 1 == 2",
"E assert 1 == 2",
"E +1",
"E -2",
"",
"pdb.py:2: AssertionError",
"*= 1 failed in *",
Expand Down
48 changes: 14 additions & 34 deletions testing/test_assertion.py
Expand Up @@ -83,7 +83,7 @@ def test_dummy_failure(pytester): # how meta!
"E assert {'failed': 1,... 'skipped': 0} == {'failed': 0,... 'skipped': 0}",
"E Omitting 1 identical items, use -vv to show",
"E Differing items:",
"E Use -v to get the full diff",
"E Use -v to get more diff",
]
)
# XXX: unstable output.
Expand Down Expand Up @@ -376,7 +376,7 @@ def test_bytes_diff_normal(self) -> None:
assert diff == [
"b'spam' == b'eggs'",
"At index 0 diff: b's' != b'e'",
"Use -v to get the full diff",
"Use -v to get more diff",
]

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

def test_iterable_quiet(self) -> None:
expl = callequal([1, 2], [10, 2], verbose=-1)
assert expl == [
"[1, 2] == [10, 2]",
"At index 0 diff: 1 != 10",
"Use -v to get more diff",
]

def test_iterable_full_diff_ci(
self, monkeypatch: MonkeyPatch, pytester: Pytester
) -> None:
Expand All @@ -466,7 +474,7 @@ def test_full_diff():

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

def test_list_different_lengths(self) -> None:
expl = callequal([0, 1], [0, 1, 2])
Expand Down Expand Up @@ -699,32 +707,6 @@ def test_list_tuples(self) -> None:
assert expl is not None
assert len(expl) > 1

def test_repr_verbose(self) -> None:
class Nums:
def __init__(self, nums):
self.nums = nums

def __repr__(self):
return str(self.nums)

list_x = list(range(5000))
list_y = list(range(5000))
list_y[len(list_y) // 2] = 3
nums_x = Nums(list_x)
nums_y = Nums(list_y)

assert callequal(nums_x, nums_y) is None

expl = callequal(nums_x, nums_y, verbose=1)
assert expl is not None
assert "+" + repr(nums_x) in expl
assert "-" + repr(nums_y) in expl

expl = callequal(nums_x, nums_y, verbose=2)
assert expl is not None
assert "+" + repr(nums_x) in expl
assert "-" + repr(nums_y) in expl

def test_list_bad_repr(self) -> None:
class A:
def __repr__(self):
Expand Down Expand Up @@ -851,8 +833,6 @@ def test_recursive_dataclasses_verbose(self, pytester: Pytester) -> None:
"E ",
"E Drill down into differing attribute a:",
"E a: 10 != 20",
"E +10",
"E -20",
"E ",
"E Drill down into differing attribute b:",
"E b: 'ten' != 'xxx'",
Expand Down Expand Up @@ -1059,7 +1039,7 @@ def test_namedtuple(self) -> None:
" b: 'b' != 'c'",
" - c",
" + b",
"Use -v to get the full diff",
"Use -v to get more diff",
]

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


Expand Down
2 changes: 0 additions & 2 deletions testing/test_error_diffs.py
Expand Up @@ -231,8 +231,6 @@ def test_this():
E ['a']
E Drill down into differing attribute a:
E a: 1 != 2
E +1
E -2
""",
id="Compare data classes",
),
Expand Down