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

How to implement assertions with a tolerance #565

Closed
noahnu opened this issue Oct 27, 2021 Discussed in #537 · 4 comments
Closed

How to implement assertions with a tolerance #565

noahnu opened this issue Oct 27, 2021 Discussed in #537 · 4 comments
Labels
feature request New feature or request question

Comments

@noahnu
Copy link
Collaborator

noahnu commented Oct 27, 2021

Discussed in #537

Originally posted by rassie August 4, 2021
I would like to implement this assertion to check that count is within 10% of the expected value:

assert count <= snapshot * 1.1 and count >= snapshot * 0.9

Is there a good way to do that with syrupy?

@noahnu noahnu added feature request New feature or request question labels Oct 27, 2021
@noahnu
Copy link
Collaborator Author

noahnu commented Oct 27, 2021

Sorry about the delayed response, I didn't even realize we had discussions enabled 😓. We have notifications setup for new issues, but nothing watching the discussions.


I believe the author of #438 was trying to do the same sort of assertion as you describe just using pytest.approx instead. There are some suggestions and workarounds in that issue. Please let me know if there are any follow up questions.

In terms of that specific syntax, it's not possible with the latest syrupy, though it's interesting and may warrant further investigation. What would the underlying snapshot look like?

Originally posted by @noahnu in #537 (comment)

@noahnu
Copy link
Collaborator Author

noahnu commented Oct 27, 2021

I would assume a snapshot would just contain a number, which would be the 100% baseline. When running the test, it should match with a provided tolerance. I don't really know what would be a good syntax for that, but I suppose anything will work as long it does what's needed.

Originally posted by @rassie in #537 (reply in thread)

@noahnu
Copy link
Collaborator Author

noahnu commented Oct 27, 2021

it should match with a provided tolerance

The difficulty is that we only serialize data. We don't currently support deserialization. So we do a character by character match on the serialized data. This ensures the snapshots are 100% deterministic (unlike snapshottest which writes python that can include memory addresses for example and thus produce inconsistent results between snapshots).

Originally posted by @noahnu in #537 (reply in thread)

@filosfino
Copy link

@noahnu noahnu closed this as not planned Won't fix, can't repro, duplicate, stale May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request question
Projects
None yet
Development

No branches or pull requests

2 participants