-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(snapshots): move to separate module (#152)
- Loading branch information
Showing
12 changed files
with
189 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import inspect | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
|
||
proj_root = Path(__file__).parents[1] | ||
module_path = Path(inspect.getmodulename(__file__)) | ||
pytest_plugins = [ "modflow_devtools.snapshots" ] # activate snapshot fixtures | ||
snapshot_array = np.array([1.1, 2.2, 3.3]) | ||
snapshots_path = proj_root / "autotest" / "__snapshots__" | ||
|
||
|
||
def test_binary_array_snapshot(array_snapshot): | ||
assert array_snapshot == snapshot_array | ||
snapshot_path = ( | ||
snapshots_path | ||
/ module_path.stem | ||
/ f"{inspect.currentframe().f_code.co_name}.npy" | ||
) | ||
assert snapshot_path.is_file() | ||
assert np.allclose(np.load(snapshot_path), snapshot_array) | ||
|
||
|
||
# todo: reinstate if/when we support multiple arrays | ||
# def test_binary_array_snapshot_multi(array_snapshot): | ||
# arrays = {"ascending": snapshot_array, "descending": np.flip(snapshot_array)} | ||
# assert array_snapshot == arrays | ||
# snapshot_path = ( | ||
# snapshots_path | ||
# / module_path.stem | ||
# / f"{inspect.currentframe().f_code.co_name}.npy" | ||
# ) | ||
# assert snapshot_path.is_file() | ||
# assert np.allclose(np.load(snapshot_path)["ascending"], snapshot_array) | ||
# assert np.allclose(np.load(snapshot_path)["descending"], np.flip(snapshot_array)) | ||
|
||
|
||
def test_text_array_snapshot(text_array_snapshot): | ||
assert text_array_snapshot == snapshot_array | ||
snapshot_path = ( | ||
snapshots_path | ||
/ module_path.stem | ||
/ f"{inspect.currentframe().f_code.co_name}.txt" | ||
) | ||
assert snapshot_path.is_file() | ||
assert np.allclose(np.loadtxt(snapshot_path), snapshot_array) | ||
|
||
|
||
def test_readable_text_array_snapshot(readable_array_snapshot): | ||
assert readable_array_snapshot == snapshot_array | ||
snapshot_path = ( | ||
snapshots_path | ||
/ module_path.stem | ||
/ f"{inspect.currentframe().f_code.co_name}.txt" | ||
) | ||
assert snapshot_path.is_file() | ||
assert np.allclose( | ||
np.fromstring( | ||
open(snapshot_path).readlines()[0].replace("[", "").replace("]", ""), | ||
sep=" ", | ||
), | ||
snapshot_array, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Snapshot testing | ||
|
||
Snapshot testing is a form of regression testing in which a "snapshot" of the results of some computation is verified and captured by the developer to be compared against when tests are subsequently run. This is accomplished with [`syrupy`](https://github.com/tophat/syrupy), which provides a `snapshot` fixture overriding the equality operator to allow comparison with e.g. `snapshot == result`. A few custom fixtures for snapshots of NumPy arrays are also provided: | ||
|
||
- `array_snapshot`: saves an array in a binary file for compact storage, can be inspected programmatically with `np.load()` | ||
- `text_array_snapshot`: flattens an array and stores it in a text file, compromise between readability and disk usage | ||
- `readable_array_snapshot`: stores an array in a text file in its original shape, easy to inspect but largest on disk | ||
|
||
By default, tests run in comparison mode. This means a newly written test using any of the snapshot fixtures will fail until a snapshot is created. Snapshots can be created/updated by running pytest with the `--snapshot-update` flag. | ||
|
||
## Using snapshot fixtures | ||
|
||
To use snapshot fixtures, add the following line to a test file or `conftest.py` file: | ||
|
||
```python | ||
pytest_plugins = [ "modflow_devtools.snapshots" ] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.