Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assertion exception shows error at correct location #402

Merged
merged 3 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/syrupy/assertion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import traceback
from gettext import gettext
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -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] = []
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/syrupy/extensions/amber/test_amber_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down