Skip to content

Commit 9f9e4be

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

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-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:

tests/test_snapshots_integration.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
class TestSnapshotIntegration:
25+
@pytest.mark.skip_snapshot_verify(paths=["$..id"])
26+
def test_skip_id_field_passes(self, snapshot):
27+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
28+
snapshot.match("user", {"name": "John", "id": "new"})
29+
30+
# HACK(gregfurman): xfail(strict=True) means we expect the test to fail -- where the underlying test failing
31+
# results in an expected XFAIL, skipping the test. Otherwise, a PASS should trigger a true FAIL.
32+
@pytest.mark.xfail(strict=True, reason="Should fail because name differs, only ID is skipped")
33+
@pytest.mark.skip_snapshot_verify(paths=["$..id"])
34+
def test_skip_id_but_name_differs_fails(self, snapshot):
35+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
36+
snapshot.match("user", {"name": "Jane", "id": "new"})
37+
38+
@pytest.mark.xfail(strict=True, reason="Should fail because name differs, only ID is skipped")
39+
@pytest.mark.skip_snapshot_verify(["$..id"])
40+
def test_skip_id_field_passes_args(self, snapshot):
41+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
42+
snapshot.match("user", {"name": "Jane", "id": "new"})
43+
44+
@pytest.mark.xfail(strict=True, reason="Should fail because no fields are skipped")
45+
def test_no_skip_marker_fails(self, snapshot):
46+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
47+
snapshot.match("user", {"name": "John", "id": "new"})
48+
49+
@pytest.mark.skip_snapshot_verify(paths=["$..id", "$..timestamp"])
50+
def test_skip_multiple_fields_passes(self, snapshot):
51+
snapshot.recorded_state = {"event": {"type": "login", "id": "123", "timestamp": "old"}}
52+
snapshot.match("event", {"type": "login", "id": "456", "timestamp": "new"})
53+
54+
@pytest.mark.skip_snapshot_verify(condition=lambda: True)
55+
def test_condition_true_skips_all_verification(self, snapshot):
56+
snapshot.recorded_state = {"data": "old"}
57+
snapshot.match("data", "completely_different")
58+
59+
@pytest.mark.skip_snapshot_verify(condition=lambda: False, paths=["$..id"])
60+
def test_condition_false_ignores_paths(self, snapshot):
61+
snapshot.recorded_state = {"user": {"name": "John", "id": "123"}}
62+
snapshot.match("user", {"name": "John", "id": "123"})
63+
64+
@pytest.mark.skip_snapshot_verify(["$..id"], lambda: True)
65+
def test_condition_with_args_skips_all(self, snapshot):
66+
snapshot.recorded_state = {"data": {"id": "old"}}
67+
snapshot.match("data", {"id": "new"})
68+
69+
@pytest.mark.xfail(strict=True, reason="Should fail because condition is False")
70+
@pytest.mark.skip_snapshot_verify(["$..id"], lambda: False)
71+
def test_condition_false_with_args_fails(self, snapshot):
72+
snapshot.recorded_state = {"user": {"name": "John", "id": "old"}}
73+
snapshot.match("user", {"name": "John", "id": "new"})

0 commit comments

Comments
 (0)