GeezerVison is showing github.com โ (๐) color the diff of strings ยท Issue #13175 ยท pytest-dev/pytest
(๐) color the diff of strings #13175Descriptiondef test_list():
assert [1, 2, 3] == [1, 4, 3]
def test_str():
assert "asdf" == "asgf"
Warning it is CRITICAL that pygments is installed, otherwise the diff won't be colored at all
this is a secret undocumented feature, see: #13174
> uv init
> uv add pytest pygments
...
> uv run pytest -vv
FAILED test_test.py::test_list - assert [1, 2, 3] == [1, 4, 3]
At index 1 diff: 2 != 4
Full diff:
[
1,
- 4,
? ^
+ 2,
? ^
3,
]
FAILED test_test.py::test_str - AssertionError: assert 'asdf' == 'asgf'
- asgf
? ^
+ asdf
? ^

here we can observe that no coloring has been applied to the string diff
- a detailed description of the bug or problem you are having
- output of
pip list from the virtual environment you are using
> uv tree
uv tree
Resolved 7 packages in 4ms
test v1
โโโ pygments v2.19.1
โโโ pytest v8.3.4
โโโ colorama v0.4.6
โโโ iniconfig v2.0.0
โโโ packaging v24.2
โโโ pluggy v1.5.0
- pytest and operating system versions
windows 10
- minimal example if possible
Reacted by DetachHead, Freya Bruhin and 5j9 ActivityThe-Compiler commented on Jan 30, 2025 Huh, interesting!
Here is a quick hack that extends _diff_text the same way other diffing functions were in fbe3e29:
diff --git i/src/_pytest/assertion/util.py w/src/_pytest/assertion/util.py
index 3fe7eb9d8..9a7c6da72 100644
--- i/src/_pytest/assertion/util.py
+++ w/src/_pytest/assertion/util.py
@@ -242,7 +242,7 @@ def _compare_eq_any(
) -> list[str]:
explanation = []
if istext(left) and istext(right):
- explanation = _diff_text(left, right, verbose)
+ explanation = _diff_text(left, right, highlighter, verbose)
else:
from _pytest.python_api import ApproxBase
@@ -274,7 +274,9 @@ def _compare_eq_any(
return explanation
-def _diff_text(left: str, right: str, verbose: int = 0) -> list[str]:
+def _diff_text(
+ left: str, right: str, highlighter: _HighlightFunc, verbose: int = 0
+) -> list[str]:
"""Return the explanation for the diff between text.
Unless --verbose is used this will skip leading and trailing
@@ -315,10 +317,15 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> list[str]:
explanation += ["Strings contain only whitespace, escaping them using repr()"]
# "right" is the expected base against which we compare "left",
# see https://github.com/pytest-dev/pytest/issues/3333
- explanation += [
- line.strip("\n")
- for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
- ]
+ explanation.extend(
+ highlighter(
+ "\n".join(
+ line.strip("\n")
+ for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
+ ),
+ lexer="diff",
+ ).splitlines()
+ )
return explanation
and from a quick test, that seems to help:

@BenjaminSchubert was there a reason you didn't do _diff_text as part of #11530 as well, or was that just an oversight perhaps? BenjaminSchubert commented on Feb 1, 2025 Hey!
This was an oversight, I rarely use the direct string diff and thus did not realize I missed something. I think it's a good idea to extend it here too! added a commit that references this issue on Feb 3, 2025This issue will close once commit 3378a16 is merged into the 'master' branch.3378a16 The-Compiler commented on Feb 3, 2025 Reacted by KotlinIsland Reacted by KotlinIsland added 3 commits that reference this issue on Feb 3, 2025This issue will close once commit 44e9f68 is merged into the 'master' branch.44e9f68 This issue will close once commit ca6557e is merged into the 'master' branch.ca6557e This issue will close once commit cb089b6 is merged into the 'master' branch.cb089b6 added a commit that references this issue on Feb 9, 2025This issue will close once commit a3d55a6 is merged into the 'main' branch.a3d55a6
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|