diff --git a/changelog/10704.bugfix.rst b/changelog/10704.bugfix.rst new file mode 100644 index 00000000000..abe9b77f81e --- /dev/null +++ b/changelog/10704.bugfix.rst @@ -0,0 +1 @@ +Show whitespace escape codes (such as ``\t``, ``\r``, ``\v``, ``\f``, ``\u00a0``) in string comparison diffs when non-printable whitespace characters differ. diff --git a/src/_pytest/assertion/compare_text.py b/src/_pytest/assertion/compare_text.py index 9bee86a0b93..62310473e2e 100644 --- a/src/_pytest/assertion/compare_text.py +++ b/src/_pytest/assertion/compare_text.py @@ -11,6 +11,20 @@ from _pytest.compat import assert_never +_WHITESPACE_MAP = { + "\t": r"\t", + "\r": r"\r", + "\v": r"\v", + "\f": r"\f", + "\u00a0": r"\u00a0", +} + + +def _escape_whitespace(text: str) -> str: + """Replace hidden whitespace characters with their literal escape representations.""" + return "".join(_WHITESPACE_MAP.get(ch, ch) for ch in text) + + def _compare_eq_text( left: str, right: str, @@ -86,12 +100,19 @@ def _diff_text( left = repr(str(left)) right = repr(str(right)) yield "Strings contain only whitespace, escaping them using repr()" + left_lines = _cap_ndiff_input(left, keepends, truncation_budget) right_lines = _cap_ndiff_input(right, keepends, truncation_budget) + + # Escape invisible whitespace characters (\t, \r, \v, \f, \u00a0) while + # preserving the original line-break structure for diff display. # "right" is the expected base against which we compare "left", # see https://github.com/pytest-dev/pytest/issues/3333 yield from highlighter( - "\n".join(line.strip("\n") for line in ndiff(right_lines, left_lines)), + "\n".join( + _escape_whitespace(line.strip("\n")) + for line in ndiff(right_lines, left_lines) + ), lexer="diff", ).splitlines() diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 035aad31c54..71be8f2d5ec 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -3155,3 +3155,54 @@ def test_order(): "test_order.py:*: AssertionError", ] ) + + +@pytest.mark.parametrize( + ("actual", "expected", "escape"), + [ + pytest.param("hello\tworld", "hello world", r"\t", id="tab"), + pytest.param("hello\tworld", "hello earth", r"\t", id="whitespace-and-text"), + pytest.param("hello\vworld", "hello world", r"\v", id="vertical-tab"), + pytest.param("hello\fworld", "hello world", r"\f", id="form-feed"), + pytest.param("hello\r\nworld", "hello\nworld", r"\r", id="carriage-return"), + pytest.param("hello\t", "hello ", r"\t", id="trailing-tab-vs-space"), + pytest.param("hello world", "hello\u00a0world", r"\u00a0", id="nbsp"), + ], +) +def test_reprcompare_whitespace_inside_text( + actual: str, expected: str, escape: str +) -> None: + lines = callequal(actual, expected) + assert lines is not None + + diff_lines = [line for line in lines if line.startswith(("+", "-"))] + assert diff_lines + assert any(escape in line for line in diff_lines) + assert not any(char in line for line in diff_lines for char in "\t\r\v\f") + + +def test_reprcompare_whitespace_inside_text_identical() -> None: + lines = callequal("hello\tworld", "hello\tworld") + assert lines is not None + assert not any(line.startswith(("+", "-")) for line in lines) + + +def test_whitespace_diff_pytester(pytester: Pytester) -> None: + """Garante que códigos de escape para whitespace invisível sejam visíveis na saída do terminal (#10704).""" + p = pytester.makepyfile( + """ + def test_whitespace(): + expected = "hello\tworld" + actual = "hello world" + assert actual == expected + """ + ) + result = pytester.runpytest(str(p)) + result.stdout.fnmatch_lines( + [ + "*AssertionError: assert*", + "*- hello\\tworld*", + "*+ hello world*", + "*1 failed in*", + ] + )