diff --git a/AUTHORS b/AUTHORS index 80b6d5157..89ed7c3f1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -136,6 +136,7 @@ Eero Vaher Eli Boyarski Elizaveta Shashkova Éloi Rivard +Emmeline Wetzel Endre Galaczi Eric Hunsberger Eric Liu @@ -247,6 +248,7 @@ Loic Esteve Lukas Bednar Luke Murphy Maciek Fijalkowski +Madeline Thai-Tang Maho Maik Figura Mandeep Bhutani diff --git a/changelog/10704.improvement.rst b/changelog/10704.improvement.rst new file mode 100644 index 000000000..81978807c --- /dev/null +++ b/changelog/10704.improvement.rst @@ -0,0 +1 @@ +Changed the diff explanation for string comparison assertions when there is a tab in the string to show ``\t`` instead of four spaces. diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index a118befcc..7644dd272 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -319,6 +319,10 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]: line.strip("\n") for line in ndiff(right.splitlines(keepends), left.splitlines(keepends)) ] + for i in range(len(explanation)): + if "? " not in explanation[i]: # dont replace diff message tab + explanation[i] = explanation[i].replace("+ ", "+ \\t") + explanation[i] = explanation[i].replace(" ", "\\t") return explanation diff --git a/testing/test_error_diffs.py b/testing/test_error_diffs.py index f290eb167..552e0c1e8 100644 --- a/testing/test_error_diffs.py +++ b/testing/test_error_diffs.py @@ -282,6 +282,23 @@ TESTCASES = [ """, id="Compare attrs classes", ), + pytest.param( + """ + def test_this(): + result = '''spam bacon + eggs love''' + desired = "spam bacon eggs love" + assert result == desired + """, + """ + > assert result == desired + E AssertionError: assert 'spam bacon\\n eggs love' == 'spam bacon eggs love' + E - spam bacon eggs love + E + spam\\tbacon + E + \\teggs love + """, + id="Test tab repr in diff", + ), ]