Skip to main content

PyTest-Locker

Example

PyTest-Locker: The fastest way to check for unexpected changes between test runs

actions batch pypi python versions codecov License: MIT Code style: black

The general concept

In essense Pytest-Locker changes the basis of testing from having to assert everything that is relevant about an object to only having to assert that an object should not change unexpectedly (i.e. the object is locked).

This, of course, implies that the pytest-locker approach makes a lot of sense when the assertion logic becomes complex. I found it especially handy when testing if I'm sending the right API calls.

Since objects can be just about anything in python (output, state, or even function calls via mocking) you can use this approach for just about everything.

Since you need to validate if the object to lock is correct, both in the first run and after desired modifications, the test flow is slightly different:

pytest-locker's flow diagram

Why use PyTest-Locker

  • Time efficient: No need to hard code expected responses. (Especially usefull for data heavy unittests)

  • Easy to verify changes:

    • Seperates the logic of the test from the expected values.
    • The lock files (containing the expected values), and changes to them, are easy to interpret. This makes it really simple to evaluate changes during testing, in commits and in pull request.

Install

run pip install pytest-locker

Usage

Configuring the project and writing your first test.

  1. Add from pytest_locker import locker to your conftest.py file
  2. To access the locker by adding it to the method parameters i.e. def test_example(locker)
  1. Use locker.lock(your_string, optional_name) to lock the data (of-course you can also lock other types).
  2. Ensure that the pytest rootdir is fixed. See the pytest customize documentation for all the options (one is adding a pytest.ini to the root folder)
  3. Ensure that .pytest_locker/ is synced via git, to ensure that you, your team, and your CI/CD pipelines are working with the same data.

And you're all set!

Accepting the current behavior and checking fo changes in this behavior

There are two modes based on for locking. The first is

  1. When user input is allowed, i.e. when running pytest with --capture no or -s

    When user input is allowed and the given data does not correspond to the data in the lock the user is prompted if the new data should be stored or if the tests should fail.

  2. When user input is captured which is default behavior for pytest

    If user input is not allowed the tests will automatically fail if the expected lock file does not exist or if the data does not correspond to the data in the lock file.

The Locker class

You can also use pytest_locker.Locker (i.e. the class of which the locker fixture returns an instance) directly to create fixtures that locks a (non-string) object without needing to turn the object into a string it.

Examples

For example of use look at the tests in repr-utils.

Release files for pytest-locker 0.3.0

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

Source distribution (sdist)

Source distribution for pytest-locker 0.3.0
File Size Uploaded
pytest_locker-0.3.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-locker 0.3.0
File Interpreter ABI Platform
pytest_locker-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / pytest_locker-0.3.0.tar.gz

Download URL pytest_locker-0.3.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
973748990c2e5b2b014068fbfa1356ee23d5d17a17756d20054ffd0d30086edc
BLAKE2b-256 checksum
How to use checksums
d119e1de1e1b7057331086d908078dfabecbd67e705e09c502e4e2a3a03a8b6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.8 Linux/6.8.0-1017-azure

Release files / pytest_locker-0.3.0-py3-none-any.whl

Download URL pytest_locker-0.3.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89f9bf518beb93ff123f8abcf46b5b94160498b9b2fbbe4a86e6a29e66bbb33e
BLAKE2b-256 checksum
How to use checksums
b702e6bd4d778b82f2203c20266b790762c45936650b5d959c46ca934f5c6c99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.8 Linux/6.8.0-1017-azure

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.5

2 release files

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