Skip to content

Commit 4e289ee

Browse files
authored
feat(snapshots): add --snapshot-disable cli option (#157)
* add --snapshot-disable pytest CLI option to disable snapshot comparisons * if snapshots are disabled, the snapshot fixture provided to the test function will attest equivalence to anything, e.g. snapshot == "yes, anything" returns True
1 parent 0e3120a commit 4e289ee

4 files changed

Lines changed: 109 additions & 9 deletions

File tree

autotest/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from pathlib import Path
22

3-
pytest_plugins = ["modflow_devtools.fixtures"]
3+
pytest_plugins = ["modflow_devtools.fixtures", "modflow_devtools.snapshots"]
44
project_root_path = Path(__file__).parent

autotest/test_snapshots.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
from pathlib import Path
33

44
import numpy as np
5+
import pytest
6+
from _pytest.config import ExitCode
57

68
proj_root = Path(__file__).parents[1]
79
module_path = Path(inspect.getmodulename(__file__))
8-
pytest_plugins = ["modflow_devtools.snapshots"] # activate snapshot fixtures
910
snapshot_array = np.array([1.1, 2.2, 3.3])
1011
snapshots_path = proj_root / "autotest" / "__snapshots__"
1112

@@ -61,3 +62,47 @@ def test_readable_text_array_snapshot(readable_array_snapshot):
6162
),
6263
snapshot_array,
6364
)
65+
66+
67+
@pytest.mark.meta("test_snapshot_disable")
68+
def test_snapshot_disable_inner(snapshot):
69+
assert snapshot == "match this!"
70+
71+
72+
@pytest.mark.parametrize("disable", [True, False])
73+
def test_snapshot_disable(disable):
74+
inner_fn = test_snapshot_disable_inner.__name__
75+
args = [
76+
__file__,
77+
"-v",
78+
"-s",
79+
"-k",
80+
inner_fn,
81+
"-M",
82+
"test_snapshot_disable",
83+
]
84+
if disable:
85+
args.append("--snapshot-disable")
86+
assert pytest.main(args) == (ExitCode.OK if disable else ExitCode.TESTS_FAILED)
87+
88+
89+
@pytest.mark.meta("test_array_snapshot_disable")
90+
def test_array_snapshot_disable_inner(array_snapshot):
91+
assert array_snapshot == "can you match that?"
92+
93+
94+
@pytest.mark.parametrize("disable", [True, False])
95+
def test_array_snapshot_disable(disable):
96+
inner_fn = test_array_snapshot_disable_inner.__name__
97+
args = [
98+
__file__,
99+
"-v",
100+
"-s",
101+
"-k",
102+
inner_fn,
103+
"-M",
104+
"test_array_snapshot_disable",
105+
]
106+
if disable:
107+
args.append("--snapshot-disable")
108+
assert pytest.main(args) == (ExitCode.OK if disable else ExitCode.TESTS_FAILED)

docs/md/snapshots.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ To use snapshot fixtures, add the following line to a test file or `conftest.py`
1414

1515
```python
1616
pytest_plugins = [ "modflow_devtools.snapshots" ]
17-
```
17+
```
18+
19+
## Disable snapshots
20+
21+
Snapshot comparisons can be disabled by invoked `pytest` with the `--snapshot-disable` flag.

modflow_devtools/snapshots.py

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
syrupy = import_optional_dependency("syrupy")
99

1010
# ruff: noqa: E402
11+
from syrupy import __import_extension
12+
from syrupy.assertion import SnapshotAssertion
1113
from syrupy.extensions.single_file import (
1214
SingleFileSnapshotExtension,
1315
WriteMode,
1416
)
17+
from syrupy.location import PyTestLocation
1518
from syrupy.types import (
1619
PropertyFilter,
1720
PropertyMatcher,
@@ -90,19 +93,67 @@ def serialize(
9093
return np.array2string(data, threshold=np.inf)
9194

9295

96+
class MatchAnything:
97+
def __eq__(self, _):
98+
return True
99+
100+
93101
# fixtures
94102

95103

104+
@pytest.fixture(scope="session")
105+
def snapshot_disable(pytestconfig) -> bool:
106+
return pytestconfig.getoption("--snapshot-disable")
107+
108+
96109
@pytest.fixture
97-
def array_snapshot(snapshot):
98-
return snapshot.use_extension(BinaryArrayExtension)
110+
def snapshot(request, snapshot_disable) -> "SnapshotAssertion":
111+
return (
112+
MatchAnything()
113+
if snapshot_disable
114+
else SnapshotAssertion(
115+
update_snapshots=request.config.option.update_snapshots,
116+
extension_class=__import_extension(request.config.option.default_extension),
117+
test_location=PyTestLocation(request.node),
118+
session=request.session.config._syrupy,
119+
)
120+
)
99121

100122

101123
@pytest.fixture
102-
def text_array_snapshot(snapshot):
103-
return snapshot.use_extension(TextArrayExtension)
124+
def array_snapshot(snapshot, snapshot_disable):
125+
return (
126+
MatchAnything()
127+
if snapshot_disable
128+
else snapshot.use_extension(BinaryArrayExtension)
129+
)
104130

105131

106132
@pytest.fixture
107-
def readable_array_snapshot(snapshot):
108-
return snapshot.use_extension(ReadableArrayExtension)
133+
def text_array_snapshot(snapshot, snapshot_disable):
134+
return (
135+
MatchAnything()
136+
if snapshot_disable
137+
else snapshot.use_extension(TextArrayExtension)
138+
)
139+
140+
141+
@pytest.fixture
142+
def readable_array_snapshot(snapshot, snapshot_disable):
143+
return (
144+
MatchAnything()
145+
if snapshot_disable
146+
else snapshot.use_extension(ReadableArrayExtension)
147+
)
148+
149+
150+
# pytest config hooks
151+
152+
153+
def pytest_addoption(parser):
154+
parser.addoption(
155+
"--snapshot-disable",
156+
action="store_true",
157+
default=False,
158+
help="Disable snapshot comparisons.",
159+
)

0 commit comments

Comments
 (0)