Skip to content

Commit 59920c0

Browse files
committed
fix(snapshot): Support args with skip_snapshot_verify marker
1 parent 1fd8000 commit 59920c0

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

localstack_snapshot/pytest/snapshot.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,17 @@ def pytest_runtest_call(item: Item) -> None:
7171
if not is_aws(): # only skip for local tests
7272
for m in item.iter_markers(name="skip_snapshot_verify"):
7373
skip_paths = m.kwargs.get("paths", [])
74-
7574
skip_condition = m.kwargs.get("condition")
75+
76+
if not (skip_paths or skip_condition) and m.args:
77+
(skip_paths, *_skip_condition) = m.args
78+
if _skip_condition:
79+
skip_condition, *_ = _skip_condition
80+
81+
if skip_paths:
82+
if not isinstance(skip_paths, list):
83+
raise ValueError("paths must be a list")
84+
7685
# can optionally include a condition, when this will be skipped
7786
# a condition must be a Callable returning something truthy/falsey
7887
if skip_condition:
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import os
2+
import tempfile
3+
4+
import pytest
5+
6+
from localstack_snapshot.snapshots import SnapshotSession
7+
8+
pytest_plugins = [
9+
"localstack_snapshot.pytest.snapshot",
10+
]
11+
12+
13+
@pytest.fixture
14+
def snapshot():
15+
with tempfile.TemporaryDirectory() as temp_dir:
16+
session = SnapshotSession(
17+
scope_key="test",
18+
verify=True,
19+
base_file_path=os.path.join(temp_dir, "test"),
20+
update=False,
21+
)
22+
yield session
23+
24+
25+
class TestSnapshotIntegration:
26+
@pytest.mark.skip_snapshot_verify(paths=["$..id"])
27+
def test_skip_id_field_passes(self, snapshot):
28+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
29+
snapshot.match("user", {"name": "John", "id": "new"})
30+
31+
# HACK(gregfurman): xfail(strict=True) means we expect the test to fail -- where the underlying test failing
32+
# results in an expected XFAIL, skipping the test. Otherwise, a PASS should trigger a true FAIL.
33+
@pytest.mark.xfail(strict=True, reason="Should fail because name differs, only ID is skipped")
34+
@pytest.mark.skip_snapshot_verify(paths=["$..id"])
35+
def test_skip_id_but_name_differs_fails(self, snapshot):
36+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
37+
snapshot.match("user", {"name": "Jane", "id": "new"})
38+
39+
@pytest.mark.xfail(strict=True, reason="Should fail because name differs, only ID is skipped")
40+
@pytest.mark.skip_snapshot_verify(["$..id"])
41+
def test_skip_id_field_passes_args(self, snapshot):
42+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
43+
snapshot.match("user", {"name": "Jane", "id": "new"})
44+
45+
@pytest.mark.xfail(strict=True, reason="Should fail because no fields are skipped")
46+
def test_no_skip_marker_fails(self, snapshot):
47+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
48+
snapshot.match("user", {"name": "John", "id": "new"})
49+
50+
@pytest.mark.skip_snapshot_verify(paths=["$..id", "$..timestamp"])
51+
def test_skip_multiple_fields_passes(self, snapshot):
52+
snapshot.recorded_state = {"event": {"type": "login", "id": "123", "timestamp": "old"}}
53+
snapshot.match("event", {"type": "login", "id": "456", "timestamp": "new"})
54+
55+
@pytest.mark.skip_snapshot_verify(condition=lambda: True)
56+
def test_condition_true_skips_all_verification(self, snapshot):
57+
snapshot.recorded_state = {"data": "old"}
58+
snapshot.match("data", "completely_different")
59+
60+
@pytest.mark.skip_snapshot_verify(condition=lambda: False, paths=["$..id"])
61+
def test_condition_false_ignores_paths(self, snapshot):
62+
snapshot.recorded_state = {"user": {"name": "John", "id": "123"}}
63+
snapshot.match("user", {"name": "John", "id": "123"})
64+
65+
@pytest.mark.skip_snapshot_verify(["$..id"], lambda: True)
66+
def test_condition_with_args_skips_all(self, snapshot):
67+
snapshot.recorded_state = {"data": {"id": "old"}}
68+
snapshot.match("data", {"id": "new"})
69+
70+
@pytest.mark.xfail(strict=True, reason="Should fail because condition is False")
71+
@pytest.mark.skip_snapshot_verify(["$..id"], lambda: False)
72+
def test_condition_false_with_args_fails(self, snapshot):
73+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
74+
snapshot.match("user", {"name": "John", "id": "new"})

0 commit comments

Comments
 (0)