Used to lock object during testing. Essentially changing assertions from being hard coded to asserting that nothing changed
Project description
PyTest-Locker
PyTest-Locker: The fastest way to check for unexpected changes between test runs
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:
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.
- Add
from pytest_locker import locker
to your conftest.py file - To access the locker by adding it to the method parameters i.e.
def test_example(locker)
- Use
locker.lock(your_string, optional_name)
to lock the data (of-course you can also lock other types). - 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) - 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
-
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 repr-utils.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-locker-0.2.9.tar.gz
.
File metadata
- Download URL: pytest-locker-0.2.9.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.9.7 Linux/5.8.0-1042-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08b8c500328228c225483d586d651f899de3b71b867da1334255005ba6a97d3d |
|
MD5 | 71003ba17f49412c544f17faecea488e |
|
BLAKE2b-256 | 7f3ae3fc927b05bfbd6e60a68564ad258c91d475b33648a8656fa29431310bc4 |
File details
Details for the file pytest_locker-0.2.9-py3-none-any.whl
.
File metadata
- Download URL: pytest_locker-0.2.9-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.9.7 Linux/5.8.0-1042-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af53615b76fa2391c0a02ae84fea03e7fdf2ac6e34a996f923cc68051438d27b |
|
MD5 | 6fb46e60461265f0f9981aafeac6cf43 |
|
BLAKE2b-256 | 1bb888763fd305c6c743f92602581e5b2062e2ac8ffe2300098626b9b0efe5cc |