From f2eb920e46710777985c38822a84055c1aa56108 Mon Sep 17 00:00:00 2001 From: "Commentator2.0" Date: Fri, 13 Jun 2025 16:27:51 +0200 Subject: [PATCH] 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 --- .../functional2/testlib/fixtures/snapshot.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/functional2/testlib/fixtures/snapshot.py b/tests/functional2/testlib/fixtures/snapshot.py index 38f4dfdad..d78879533 100644 --- a/tests/functional2/testlib/fixtures/snapshot.py +++ b/tests/functional2/testlib/fixtures/snapshot.py @@ -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