Pinning tests in python using pytest

Photo by Immo Wegmann on Unsplash
  • Initial run to pin down the current behavior (store it for later comparison).
  • All subsequent runs compares the new output with the pinned output from the initial run. The test passes if the new output is identical to the pinned output and fails otherwise.
  • When refactoring, especially legacy codebases without any other test coverage.
  • When dealing with code for numerical computations, such as optimization problems, simulations, classification, regression etc. where explicit unit and integration tests are notoriously hard to write except for the very simplest, analytically solvable, cases.

pytest-pinned to the rescue!

pytest-pinned is a pytest plugin and gives you the following:

  • A single fixture to be used in your pytest test suite. pytest-pinned keeps track of in which test the fixture is used and allows for using the same fixture multiple times in a single test.
  • A single json file collecting the pinned output from your pinning tests (this one should be added to your VCS!)
  • pytest configuration flags for rewriting and updating the pinned output
pip install pytest-pinned
pytest --pinned-rewrite test_my_func.py
pytest test_my_func.py

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store