diff --git a/AUTHORS b/AUTHORS index 9094100cb4e..6b2ed8f65c4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -10,6 +10,7 @@ Abhijeet Kasurde Adam Johnson Adam Stewart Adam Uhlir +Aditya Tripuraneni Ahn Ki-Wook Akhilesh Ramakrishnan Akiomi Kamakura diff --git a/changelog/14637.bugfix.rst b/changelog/14637.bugfix.rst new file mode 100644 index 00000000000..65a6d2bd42b --- /dev/null +++ b/changelog/14637.bugfix.rst @@ -0,0 +1 @@ +Pytest now correctly skips identical trailing characters in assertion text diffs even when the compared strings differ at the beginning. diff --git a/src/_pytest/assertion/compare_text.py b/src/_pytest/assertion/compare_text.py index 31096444ba6..08e85fc5811 100644 --- a/src/_pytest/assertion/compare_text.py +++ b/src/_pytest/assertion/compare_text.py @@ -59,7 +59,7 @@ def _diff_text( left = left[i:] right = right[i:] if len(left) == len(right): - for i in range(len(left)): + for i in range(1, len(left) + 1): if left[-i] != right[-i]: break if i > 42: diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 492834ba9de..20d6fa89982 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -507,6 +507,14 @@ def test_text_skipping_trailing(self) -> None: for line in lines: assert "z" * 50 not in line + def test_text_skipping_trailing_when_prefix_differs(self) -> None: + # The trailing-skip branch must still work when the first characters differ. + lines = callequal("x" + "z" * 50, "y" + "z" * 50) + assert lines is not None + assert any("identical trailing" in line for line in lines) + for line in lines: + assert "z" * 50 not in line + def test_text_skipping_verbose(self) -> None: lines = callequal("a" * 50 + "spam", "a" * 50 + "eggs", verbose=1) assert lines is not None