snaptol

Snapshot testing for numerical results with built in optional tolerance parameters.

Published: Feb 27, 2026 by Ava Dean

Project info

licence MIT

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.

It works by providing a simple pytest fixture, snaptolshot, that the user can inject into their test functions,

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

Almost any Python object can be snapshotted, including numpy arrays. It also provides integrations directly with numpy.testing utilities, such as assert_allclose. This allows a user to maintain their current testing style, while still taking advantage of the snapshot testing functionality. Many convenience features are also implemented, such as caching failed test results so expensive tests do not need to be re-run if the result is to be re-used. The difference between snapshots can also be neatly shown.

The inspiration for this project arose from using the syrupy library for snapshot testing in other codes. One shortfall of this library that was found was that it did not handle tolerance parameters for floating-point data, nor did it provide compatibility with numpy arrays.

A pyproject.toml file was created, outlining a range of dependencies and ruff linter options. The uv tool was also used as the package and project manager. Documentation was created using Sphinx, and a range of GitHub actions were set up to automate the linter checks, testing, and deployment of the package. To run instances of pytest within the test suite itself, the pytester fixture was required which gave access to creating test files with the makepyfile method and running them with runpytest_subprocess method.

Creating this package used all the skills I’ve learnt in previous projects, and put them to the test. It allowed me to experience the full process of software development, from identifying a gap in the community, to creating a package and ultimately releasing it, of which, we did to PyPI as version 0.0.1 on 2025-11-27.

python regression testing snapshot numerical tolerance

Share