-
Notifications
You must be signed in to change notification settings - Fork 37
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
PyCharm diff comparison failure doesn't work with custom classes #675
Comments
Similar issue here with basic strings: Create snapshot from: def test_test(snapshot):
test = """Hello to
the world"""
assert test == snapshot pytest --snapshot-update Modify the string (e.g., add def test_test(snapshot):
test = """Hello to
the world!""" # Added the `!`
assert test == snapshot Yields non-helpful diff view: |
I was getting the same issue and I was looking into this. @pytest.fixture(scope='session', autouse=True)
def _patch_pycharm_diff_viewer_for_snapshots():
try:
from teamcity.diff_tools import EqualsAssertionError
except ImportError:
yield
return
old_init = EqualsAssertionError.__init__
old_init_signature = signature(old_init)
@wraps(old_init)
def new_init(self, *args, **kwargs):
# Extract the __init__ arguments as originally passed in order to
# process them later
parameters = old_init_signature.bind(self, *args, **kwargs)
parameters.apply_defaults()
expected = parameters.arguments["expected"]
actual = parameters.arguments["actual"]
real_exception = parameters.arguments["real_exception"]
if isinstance(expected, SnapshotAssertion):
snapshot = expected
elif isinstance(actual, SnapshotAssertion):
snapshot = actual
else:
snapshot = None
old_init(self, *args, **kwargs)
# No snapshot was involved in the assertion. Let the old logic do its
# thing.
if snapshot is None:
return
# Although a snapshot was involved in the assertion, it seems the error
# was a result of a non-assertion exception (Ex. `assert 1/0`).
# Therefore, We will not do anything here either.
if real_exception is not None:
return
assertion_result = snapshot.executions[snapshot.num_executions - 1]
if assertion_result.exception is not None:
return
self.expected = str(assertion_result.recalled_data)
self.actual = str(assertion_result.asserted_data)
EqualsAssertionError.__init__ = new_init
yield
EqualsAssertionError.__init__ = old_init This fixture makes it so we patch what pycharm will believe the "expected" and "actual" arguments are when a test fails. It only does it when the either the "expected" or the "actual" are snapshots, and only if the exception was an assertion error (and not an unrelated exception that may have been raised during the evaluation time). I'll open a PR to add this (behind an opt-in flag?), but I have no idea whether one of the maintainers would actually accept such an ugly patch. |
🎉 This issue has been resolved in version 4.7.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Describe the bug
When comparing a failed snapshot test on an object without a custom
__repr__
(i.e. relies on syrupy's serializer), PyCharm incorrectly displays the actual output as the default__repr__
(e.g....Mansion object at 0xBEEF2020
) instead of what syrupy serialises it to (e.g.Mansion(rooms=[Room(...)])
).This might be a PyCharm bug, or Syrupy or somehow both. Just thought I'd make this issue as it tripped me up at work for over an hour and I imagine a fair few people use PyCharm and syrupy together.
To reproduce
pip install syrupy==3.0.2
Expected behavior
This is roughly what I should see in the Comparison Failure window.
I know this is wrong because when I run
pytest --snapshot-update
, it updates the snapshot like this.Screenshots
added throughout where appropriate
Environment:
Additional context
This is similar to #572 but not the same.
The text was updated successfully, but these errors were encountered: