A Python tool for snapshot testing with numerical tolerance on floating point numbers
Project description
snaptol
Snapshot testing for numerical results with built in optional tolerance parameters.
snaptol is a pytest plugin for snapshot testing that comfortably handles tolerance with floating-point data.
It is designed for numerical, scientific, and simulation-based code, where results may often not be exactly deterministic and can depend on randomised initial conditions.
Motivation
Consider an optimisation algorithm that may have a highly complex functional landscape or involve controlled randomness. For example, Monte Carlo simulations, eigensolvers, or iterative solvers. A test may look like,
import numpy as np
def test_something(snaptolshot):
rng = np.random.default_rng(seed=123)
random_initial_conditions = rng.normal(loc=0.0, scale=1e-6, size=100)
result = very_complex_computation(random_initial_conditions)
assert snaptolshot(rtol=1e-5, atol=1e-8) == result
snaptol allows you to easily snapshot this result and provide tolerance parameters for future comparisons.
The plugin handles the storage and retrieval of data with JSON serialisation, and has support for NumPy arrays and its testing functions.
Installation
Use the package manager pip to install snaptol.
pip install snaptol
Usage
Basic usage
In a test file, add the fixture, snaptolshot,
def test_something(snaptolshot):
result = compute_something()
assert snaptolshot == result
When pytest runs, it will compare the result to the snapshot stored in file.
Using numerical tolerance
You can specify tolerances directly,
def test_something(snaptolshot):
result = compute_something()
assert snaptolshot(rtol=1e-05, atol=1e-08) == result
Or equivalently, use the match method if you prefer,
def test_something(snaptolshot):
result = compute_something()
assert snaptolshot.match(rtol=1e-05, atol=1e-08) == result
Updating snapshots
Initial run (or intentional changes)
When creating snapshot tests for the first time, or when the expected result in a test has intentionally changed, run,
pytest --snaptol-update
This writes new snapshots instead of comparing values.
Important behaviour
The --snaptol-update flag will only update tests failed in the previous run. Use the --snaptol-update-all flag to enforce an update on all tests.
There should exist only one snapshot comparison per test. If the setup data is needed to be shared between different snapshot comparisons, then consider factoring it into a fixture or helper function.
NumPy support
snaptol is able to snapshot NumPy arrays and also integrates directly with the following testing utilities,
- assert_allclose
- assert_array_almost_equal_nulp
- assert_array_max_ulp
- assert_array_equal
- assert_equal
- assert_string_equal
For example,
import numpy as np
def test_numpy_allclose(snaptolshot):
result = np.arange(100, dtype=float)
snaptolshot.assert_allclose(result)
Where any *args or **kwargs are passed directly to the corresponding NumPy function.
Cache
snaptol provides an optional caching feature that can be used during update runs.
This is particularly useful for testing suites that call expensive or time-consuming functions.
Results of tests that have failed on the previous run are stored in a cache directory on disk. To enable caching, add the following option to an update run,
pytest --snaptol-update --snaptol-use-cache
When enabled, cached data is used in place of re-running the test.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file snaptol-0.0.4.tar.gz.
File metadata
- Download URL: snaptol-0.0.4.tar.gz
- Upload date:
- Size: 102.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62b7759add1aab7cd8abb3a2a25ff824259ac837fab851568c01df1dd12577be
|
|
| MD5 |
5d012197403e7c8522e1b9c53bf7a9b4
|
|
| BLAKE2b-256 |
c3f3745fe4e9248cf7372bc3ced73d52e83185e60ffc0f3193520e69198ae0cf
|
Provenance
The following attestation bundles were made for snaptol-0.0.4.tar.gz:
Publisher:
release.yml on PlasmaFAIR/snaptol
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snaptol-0.0.4.tar.gz -
Subject digest:
62b7759add1aab7cd8abb3a2a25ff824259ac837fab851568c01df1dd12577be - Sigstore transparency entry: 1186431706
- Sigstore integration time:
-
Permalink:
PlasmaFAIR/snaptol@8cdd9b107734282a04ecfefd88c5bf34737136fc -
Branch / Tag:
refs/tags/v0.0.4 - Owner: https://github.com/PlasmaFAIR
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8cdd9b107734282a04ecfefd88c5bf34737136fc -
Trigger Event:
push
-
Statement type:
File details
Details for the file snaptol-0.0.4-py3-none-any.whl.
File metadata
- Download URL: snaptol-0.0.4-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f22a1a6f982c9d9030a36652157c966f89928c95789ef89b35fc238e7e755cff
|
|
| MD5 |
71983efa23c8d302c33f669700c48f6d
|
|
| BLAKE2b-256 |
f9673c5cb4cf7e7a8902c66dfd4b775528b813fa03bce959aedd1ab63ac03ccb
|
Provenance
The following attestation bundles were made for snaptol-0.0.4-py3-none-any.whl:
Publisher:
release.yml on PlasmaFAIR/snaptol
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snaptol-0.0.4-py3-none-any.whl -
Subject digest:
f22a1a6f982c9d9030a36652157c966f89928c95789ef89b35fc238e7e755cff - Sigstore transparency entry: 1186431715
- Sigstore integration time:
-
Permalink:
PlasmaFAIR/snaptol@8cdd9b107734282a04ecfefd88c5bf34737136fc -
Branch / Tag:
refs/tags/v0.0.4 - Owner: https://github.com/PlasmaFAIR
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8cdd9b107734282a04ecfefd88c5bf34737136fc -
Trigger Event:
push
-
Statement type: