Skip to content

Commit

Permalink
test: add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
noahnu committed Oct 22, 2021
1 parent f6c52f3 commit 1777744
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tests/integration/test_custom_comparator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import pytest


@pytest.fixture
def testcases_initial(testdir):
testdir.makeconftest(
"""
import pytest
import math
from syrupy.extensions.amber import AmberSnapshotExtension
class CustomSnapshotExtension(AmberSnapshotExtension):
def matches(self, *, serialized_data, snapshot_data):
try:
a = float(serialized_data)
b = float(snapshot_data)
return math.isclose(a, b, rel_tol=1e-5)
except:
return False
@pytest.fixture
def snapshot_custom(snapshot):
return snapshot.use_extension(CustomSnapshotExtension)
"""
)
return {
"passed": (
"""
def test_passed_custom(snapshot_custom):
assert snapshot_custom == 3.0
"""
),
"failed": (
"""
def test_passed_custom(snapshot_custom):
assert snapshot_custom == 4.0
"""
),
}


@pytest.fixture
def generate_snapshots(testdir, testcases_initial):
testdir.makepyfile(test_file=testcases_initial["passed"])
result = testdir.runpytest("-v", "--snapshot-update")
return result, testdir, testcases_initial


def test_generated_snapshots(generate_snapshots):
result = generate_snapshots[0]
result.stdout.re_match_lines((r"1 snapshot generated\."))
assert "snapshots unused" not in result.stdout.str()
assert result.ret == 0


def test_approximate_match(generate_snapshots):
testdir = generate_snapshots[1]
testdir.makepyfile(
test_file="""
def test_passed_custom(snapshot_custom):
assert snapshot_custom == 3.2
"""
)
result = testdir.runpytest("-v")
result.stdout.re_match_lines((r"test_file.py::test_passed_custom PASSED"))
assert result.ret == 0


def test_failed_snapshots(generate_snapshots):
testdir = generate_snapshots[1]
testdir.makepyfile(test_file=generate_snapshots[2]["failed"])
result = testdir.runpytest("-v")
result.stdout.re_match_lines((r"1 snapshot failed\."))
assert result.ret == 1


def test_updated_snapshots(generate_snapshots):
_, testdir, initial = generate_snapshots
testdir.makepyfile(test_file=initial["failed"])
result = testdir.runpytest("-v", "--snapshot-update")
result.stdout.re_match_lines((r"1 snapshot updated\."))
assert result.ret == 0

0 comments on commit 1777744

Please sign in to comment.