Skip to main content

pytest-pinned

PyPI version shields.io 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.
  • Scientific computing where implementation of, usually quite complex, scientific models are hard to test for specific behaviours, especially when they are used for research purposes.
  • As extra confidence boosters during refactoring.

pytest-pinned keeps all expected results from pinning tests in a single, pretty-printed 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. In addition, however, pytest-pinned>=0.2.0 natively supports numpy arrays!

Note: pytest-pinned is not compatible with pytest-xdist or any other plugin that runs test in separate subprocesses.

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) with a very simple syntax. 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.approx(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 the 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.

Release files for pytest-pinned 0.4.2

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.4.2
File Interpreter ABI Platform
pytest_pinned-0.4.2-py3-none-any.whl Python 3 none any Details

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

Download URL pytest_pinned-0.4.2-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ebf8b6cd32772e940c20871f5c09ca35d18155390e649823b8435299a25bd88e
BLAKE2b-256 checksum
How to use checksums
4a0898aad61bab14108d5dc6651d121994f955bd39fdd413bf8a2a2dc4d2ce7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.4.2 This release

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

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