From 2767c4c1fd797f56055c54a4e3d3b56985879bcd Mon Sep 17 00:00:00 2001 From: Aditya-Tripuraneni Date: Mon, 22 Jun 2026 11:42:29 -0400 Subject: [PATCH 1/2] Fix off-by-one in trailing assertion diff skipping (#14637) --- changelog/14637.bugfix.rst | 1 + src/_pytest/assertion/compare_text.py | 2 +- testing/test_assertion.py | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelog/14637.bugfix.rst 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 From 71f3403c79d2eb6218f13ceb6c50eb6fe86649d4 Mon Sep 17 00:00:00 2001 From: Aditya-Tripuraneni Date: Mon, 22 Jun 2026 12:14:19 -0400 Subject: [PATCH 2/2] Add author --- AUTHORS | 1 + 1 file changed, 1 insertion(+) 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