question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use pytest.approx() with syrupy

See original GitHub issue

We 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
hashstatcommented, Dec 3, 2020

Thanks for all your suggestions. I was able to solve the issue by rounding the numbers to a reasonable precision before comparison.

def test_snapshot(snapshot):
    computed = compute_pi()
    assert snapshot == round(computed, 8)

And all is working as expected.

# name: test_snapshot
  3.14159265
---
0reactions
iamogbzcommented, Dec 3, 2020

@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.

import os
import pytest

pi = 3.141592653589793

def compute_pi():
    return {
        'win32': 3.141592654,
        'darwin': 3.14159265359,
    }.get(os.environ.get('PLATFORM'), 3.1415926536897)

def test_not_equal():
    assert pi != compute_pi()

def test_approx():
    assert pytest.approx(pi) == compute_pi()


def test_snapshot(snapshot):
    computed = compute_pi()
    approximated = pytest.approx(pi)
    def pi_approx_matcher(data, path):
        return approximated if data == approximated else data
    assert snapshot(matcher=pi_approx_matcher) == computed

Using your example

# name: test_snapshot
  3.141592653589793 ± 3.1e-06
---
$ PLATFORM=win32 pytest test_pi.py
test_pi.py ...                                                           [100%]
--------------------------- snapshot report summary --------------------------- 
1 snapshot passed.
============================== 3 passed in 0.03s ==============================
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found