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

pytest.approx does not work well with JSONSnapshotExtension #889

Open
pickfire opened this issue Sep 9, 2024 · 1 comment
Open

pytest.approx does not work well with JSONSnapshotExtension #889

pickfire opened this issue Sep 9, 2024 · 1 comment
Labels

Comments

@pickfire
Copy link

pickfire commented Sep 9, 2024

Describe the bug

When JSONSnapshotExtension is used, pytest.approx no longer works since it serializes the ApproxScalar with the original expected value.

To reproduce

  1. Install latest pytest and syrupy
  2. Add tests/test_hello.py, this random should not affect it since default rel 1e-6
import pytest
import random
from syrupy.extensions.json import JSONSnapshotExtension


@pytest.fixture
def snapshot_json(snapshot):
    return snapshot.use_extension(JSONSnapshotExtension)


def test_snapshot(snapshot_json):
    assert snapshot_json == pytest.approx(1000000 + random.random())
  1. Run pytest --snapshot-update
  2. See the error
============================= test session starts ==============================
platform linux -- Python 3.12.2, pytest-8.3.2, pluggy-1.5.0
rootdir: /home/project/data_collect
configfile: pyproject.toml
plugins: syrupy-4.7.1
collected 1 item

tests/test_hello.py F

=================================== FAILURES ===================================
________________________________ test_snapshot _________________________________

snapshot_json = "1000000.6413189545 ± 1.0e+00"


    def test_snapshot(snapshot_json):
>       assert snapshot_json == pytest.approx(1000000 + random.random())
E       assert [- snapshot] == [+ received]
E         - "1000000.6413189545 ± 1.0e+00"
E         + "1000000.0143782155 ± 1.0e+00"

tests/test_hello.py:13: AssertionError
--------------------------- snapshot report summary ----------------------------
1 snapshot failed.
=========================== short test summary info ============================
FAILED tests/test_hello.py::test_snapshot - assert [- snapshot] == [+ received]
============================== 1 failed in 0.08s ===============================
Finished running tests!

Environment (please complete the following information):

  • OS: Linux f63999551374 5.14.0-480.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jul 12 20:45:27 UTC 2024 x86_64 GNU/Linux
  • Syrupy Version: syrupy-4.7.1
  • Python Version: 3.12

Additional context

I had to change it to use f"±{d:.6e}" to get it working so that it will serialize it as str. I think maybe for JSON format we can manually override the _filter?

@noahnu
Copy link
Collaborator

noahnu commented Sep 25, 2024

Related:

@noahnu noahnu added feature request New feature or request tool compatibility labels Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants