From d46bba430fa74ec016402d3f521c4812baf07bf4 Mon Sep 17 00:00:00 2001 From: Emmanuel Ogbizi Date: Fri, 30 Oct 2020 10:27:10 -0400 Subject: [PATCH] fix: assertion exception shows error at correct location (#402) * fix: assertion exception prints error * chore: place exception on top the printed trace --- src/syrupy/assertion.py | 19 +++++++++++++++++++ .../extensions/amber/test_amber_matchers.py | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/syrupy/assertion.py b/src/syrupy/assertion.py index db2bddb2..9da99968 100644 --- a/src/syrupy/assertion.py +++ b/src/syrupy/assertion.py @@ -1,3 +1,4 @@ +import traceback from gettext import gettext from typing import ( TYPE_CHECKING, @@ -34,6 +35,7 @@ class AssertionResult: created: bool = attr.ib() updated: bool = attr.ib() success: bool = attr.ib() + exception: Optional[Exception] = attr.ib() @property def final_data(self) -> Optional["SerializedData"]: @@ -112,6 +114,18 @@ def _serialize(self, data: "SerializableData") -> "SerializedData": def get_assert_diff(self) -> List[str]: assertion_result = self._execution_results[self.num_executions - 1] + if assertion_result.exception: + lines = [ + line + for lines in traceback.format_exception( + assertion_result.exception.__class__, + assertion_result.exception, + assertion_result.exception.__traceback__, + ) + for line in lines.splitlines() + ] + # Rotate to place exception with message at first line + return lines[-1:] + lines[:-1] snapshot_data = assertion_result.recalled_data serialized_data = assertion_result.asserted_data or "" diff: List[str] = [] @@ -162,6 +176,7 @@ def _assert(self, data: "SerializableData") -> bool: serialized_data: Optional["SerializedData"] = None matches = False assertion_success = False + assertion_exception = None try: snapshot_data = self._recall_data(index=self.num_executions) serialized_data = self._serialize(data) @@ -173,6 +188,9 @@ def _assert(self, data: "SerializableData") -> bool: ) assertion_success = True return assertion_success + except Exception as e: + assertion_exception = e + return False finally: snapshot_created = snapshot_data is None and assertion_success snapshot_updated = matches is False and assertion_success @@ -184,6 +202,7 @@ def _assert(self, data: "SerializableData") -> bool: success=assertion_success, created=snapshot_created, updated=snapshot_updated, + exception=assertion_exception, ) self._executions += 1 self._post_assert() diff --git a/tests/syrupy/extensions/amber/test_amber_matchers.py b/tests/syrupy/extensions/amber/test_amber_matchers.py index 93fd2780..303bcc99 100644 --- a/tests/syrupy/extensions/amber/test_amber_matchers.py +++ b/tests/syrupy/extensions/amber/test_amber_matchers.py @@ -43,7 +43,7 @@ def test_raises_unexpected_type(snapshot): "some_uuid": uuid.uuid4(), } assert actual == snapshot(matcher=path_type(**kwargs, strict=False)) - with pytest.raises(PathTypeError, match="does not match any of the expected"): + with pytest.raises(AssertionError, match="does not match any of the expected"): assert actual == snapshot(matcher=path_type(**kwargs))