How to use pytest.approx() with syrupy
See original GitHub issueWe have a bunch of tests that perform computations using pandas, numpy, and other scientific libraries and produce a dictionary containing the resulting key-value pairs. Some of the values are slightly different when run on different platforms (i.e. macOS vs Linux), so we wrap those values with pytest.approx()
to accommodate those minor and acceptable differences.
Digging into the syrupy code, the final comparison between the test value and the snapshot is performed against serialized data, so it appears that pytest.approx()
cannot be used. Is that correct? Or can you suggest a way to allow these two great features to be used together?
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Pytest approximately equal scalars and arrays
How to cleanly compare floating point scalar and array approximate equality with Pytest.
Read more >Best practice of using unittest kind asserts (like assertNotIn) in ...
The best practice is using the language assert statement, which yields the unittest 's assert* methods unnecessary. Compare self.
Read more >API Reference — pytest documentation
The approx class performs floating-point comparisons using a syntax that's as ... When using pytest.raises() as a function, you can use: pytest.raises(Exc, ...
Read more >Matplotlib.pdf
accurate 8-spline approximation to elliptical arcs (see Arc), which are insensitive to zoom level. Bar charts. Use the bar() function to ...
Read more >Small questions about pyvista.Plotter() and FEniCS-X
In the classical FEniCS, we can use comm = mesh.mpi_comm() and MPI.min(comm, ... MIN); assert min_distance == pytest.approx(ref_distance, 1.0e-12) ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for all your suggestions. I was able to solve the issue by rounding the numbers to a reasonable precision before comparison.
And all is working as expected.
@hashstat it seems like when comparing you could approximate pi and use that compare against the computed value and then you can use a syrupy matcher to check and replace the matched value with the constant pi approximation.
Basically what @noahnu suggested but using matchers instead of a custom extension to make it terser.
Using your example