Skip to main content
actions batch pypi codecov example image

PyTest-Locker

The test-locker can be used to “lock” data from during a test. This means that rather than having to manually specify the expected output you lock the data when it corresponds to expected bahaviour.

Why use Locker

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

  • Easy to verify changes:

    • Seperates logic of the test and expected values in the test further

    • Lock files, and changes to them, are easy to interpret. Therefore, evaluting them in pull-requests a great method of quality controll.

Install

run pip install pytest-locker

Use

  • Step 1: Add from pytest_locker import locker to your conftest.py file

  • Step 2: To access the locker by adding it to the method parameters i.e. def test_example(locker)

  • Step 3: Use locker.lock(your_string, optional_name) to lock the data.

  • Additionally: Don’t forget to commit the .pytest_locker/ directory for ci/cd testing

And you’re all set!

Tip

When using locks to test your file it is even more important than usual that the pytest rootdir is fixed. Click the link for all the options (one is adding a pytest.ini to the root folder).

The Locker test Flows

There are two modes based on for locking.

  • 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.

  • 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 https://github.com/Luttik/repr_utils.

Release files for pytest-locker 0.2.4

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.2.4
File Size Uploaded
pytest-locker-0.2.4.tar.gz 4.3 kB Details

Built distribution (wheel)

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

Total release size: 8.3 kB

Release files / pytest-locker-0.2.4.tar.gz

Download URL pytest-locker-0.2.4.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
16907f97e7e75062ee7f39c43799a485940b971426799ecf9e992a5d10ddac2d
BLAKE2b-256 checksum
How to use checksums
76cacddef6e4bd257a795f087b27c16dfe5b9afd35e30312c41f8fc740f4aaa9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.2 Windows/10

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

Download URL pytest_locker-0.2.4-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89c8af9eb956cce1781bf99f0c7e30cc6517fcda5e6899e4e6c8954f7df73994
BLAKE2b-256 checksum
How to use checksums
bfd7e971c783f286d1c8bca4dbaa1ab8446fedfc3690f786c231f32b95fb1ee8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.2 Windows/10

Release history Release notifications | RSS feed

0.3.0

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

This release

0.2.4 This release

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