tests/functional2/lang: improve assertion failure message

Currently when a lang test fails, (or any snapshot assertion for that
matter) the error message is rather bulky.
This is due to both sides being printed fully, using escaped newlines
(i.e. everything is one line)

This is awful to read and check what the actual difference is. Also
there is no indication that one can update the golden files using the
cli flag.

This commit changes the error message when comparing snapshots against
something
a list of lines is shown, where the output differed. An additional note
about how to update the files automatically was added too

Change-Id: Ibedcf48018c27f924b807fbd42362fb608d27441
This commit is contained in:
Commentator2.0
2025-06-20 11:26:03 +02:00
parent 877b0d7121
commit f2eb920e46
@@ -6,6 +6,7 @@ from typing import Any
from collections.abc import Generator
import pytest
from _pytest.config import Config
from _pytest.fixtures import FixtureRequest
@@ -109,3 +110,25 @@ def snapshot(
yield create_snapshot
if any(s.did_update for s in snaps):
pytest.skip("Updated Golden Files")
def pytest_assertrepr_compare(config: Config, op: str, left: Any, right: Any) -> list[str] | None:
if not isinstance(left, Snapshot) or op != "==" or not isinstance(right, str):
return None
left: Snapshot
right: str
exp_lines = str(left).splitlines()
act_lines = right.splitlines()
expl = [
f"snapshot({left.expected_output_path.name}) == output. Consider using `--accept-tests` to update the golden files."
]
if config.get_verbosity():
expl.append("The following lines were mismatched:")
for i, (exp, act) in enumerate(zip(exp_lines, act_lines)):
if exp == act:
continue
expl.append(f"{i + 1}: - {exp}")
expl.append(f"{i + 1}: + {act}")
return expl