Skip to main content

pytest-pinned

pinned

A simple pytest plugin for writing pinning tests.

Pinning tests or snapshot tests or characterization tests are meant to describe (characterize) the actual behavior of an existing piece of software, and therefore protect existing behavior against unintended changes via automated testing.

This type of testing can come in handy for several reasons:

  • Legacy codebase with poor or non-existing test coverage
  • As broad integration tests where more specific testing may prove difficult

In my case, the main usecase is legacy and non-legacy codebases aimed for scientific computing. The implementation of, usually quite complex, scientific models are hard to test for specific behaviours, especially when they are used for research purposes. Pinning tests allow for more confident refactoring of the implementation of such models while tracing how results and predicitons changes.

pytest-pinned keeps all expected results from pinning tests in a single JSON file resulting in only a single file needing to be added to the VCS repository and diffs are also contained to this single file. The use of JSON for serialization of the expected results however imposes some restrictions on the datatypes that can be used, see the JSON type conversion table from the standard library.

Requirements

pytest-pinned has no external dependencies except for pytest itself.

Installation

You can install pytest-pinned via pip from PyPI:

$ pip install pytest-pinned

Usage

pytest-pinned expose a single pytest fixture (pinned). pinned will keep track of what test it is used in, supports usage with the standard assert statement and allows for multiple asserts in the same test.

Syntax

Simple pinning test sample:

def test_simple(pinned):
    assert(10.0 == pinned)

pytest-pinned also supports approximate comparisons using pytest.approx. See last assert statement in example below for syntax. pinned accepts the same keyword arguments as pytest.approx.

More elaborate example:

def test_elaborate(pinned):
    assert(10.0 == pinned)
    assert([1,2,3] == pinned)
    assert({'a': 1, 'b': 2} == pinned)
    assert(5.2983746239134 == pinned(rel=0.00001, abs=0.001))

Expected results

If pytest-pinned cannot find any expected results for a comparison it will fail the test and ask teh user to write new expected results.

To rewrite the expected results "from scratch", use:

$ pytest --pinned-rewrite

To update the expected results for only some tests, use:

$ pytest tests/sample_test.py::specific_test --pinned-update

To change the path where pytest-pinned stores (and loads) the expected results, use:

$ pytest --pinned-path path/to/expected/results.json

License

Distributed under the terms of the MIT license, pytest-pinned is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Contributing

Contributions are very welcome.

Metadata

Release files for pytest-pinned 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for pytest-pinned 0.1.1
File Interpreter ABI Platform Actions
pytest_pinned-0.1.1-py3-none-any.whl Python 3 none any Details for pytest_pinned-0.1.1-py3-none-any.whl

Release files / pytest_pinned-0.1.1-py3-none-any.whl

Download URL pytest_pinned-0.1.1-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2b0a8b0ef6e1e7f2a5ea7fcbf64b962863fcfca469ade21cc1ba0754079756d
BLAKE2b-256 checksum
How to use checksums
9b830cd897be0372fd4659d62ed3a10c350dd847a8920dd0d2c8173a5aaafb4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Security for pytest-pinned 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page