A pytest plugin to find dependencies between tests
Project description
A pytest plugin to find dependencies between tests.
This pytest plugin was generated with Cookiecutter along with @hackebrot’s cookiecutter-pytest-plugin template.
Summary
Tests shall generally not depend on each other. To ensure this, plugins like pytest-randomly or pytest-reverse are commonly used. These plugins find dependent tests, but it is up to you to find which test they are depend upon.
The plugin aims to automate this task. Dependencies are found in the first place by running all tests in forward and backwards direction and checking if any tests fail if executed in one order but not in the other. This will find most (but not all) test dependencies (the same way as pytest-reverse). If any dependent test is found, more test runs with a subset of all tests are run using binary search, until a test is found that causes the other test to fail.
Running tests this way may be time-consuming, especially with many tests, so it is recommended to run this only once in a while.
Installation
You can install pytest-find-dependencies via pip from PyPI:
$ pip install pytest-find-dependencies
Usage
If the plugin is installed, it can be used by adding the pytest option –find-dependencies. After running all needed tests, all found dependencies are listed. Here is an example:
================================================= Run dependency analysis for 7 tests. Executed 19 tests in 4 test runs. Dependent tests: test_one.py::test_b depends on test_one.py::test_e =================================================
In this case 7 tests have been analyzed, one dependent test has been found after running the tests forwards and backwards, and after 2 additional test runs with an overall of 5 tests, the test it depended on was found.
Some dependencies can be due to a permanent change in the environment (for example by adding a change to a database that is not reverted in subsequent test runs). In this case, the dependency cannot be found reliably, and these tests are listed separately:
================================================= Run dependency analysis for 5 tests. Executed 11 tests in 3 test runs. Tests failing permanently after all tests have run: test_one.py::test_b =================================================
Dependencies due to a permanent change will only be found if the offending test is run before the dependent test, otherwise the test will just fail both times.
The option --reversed-first allows you to reverse the sequence of the first two test runs.
The option --markers-to-ignore allows to define a comma-separated list of marker names. Tests that have these markers will be ignored in the analysis (e.g. not run at all). This can be used to exclude tests that have markers that define the test order. Examples include dependency (from the pytest-dependency plugin), order (from pytest-order) or depends (from pytest-depends). To ignore all tests with a dependency marker, you can use:
python -m pytest --find-dependencies --markers-to-ignore=dependency
Note that in this case you also won’t find other tests depending on the ordered markers.
Notes
command line options given in the test are passed to all test runs in dependency find mode
if any dependent tests are found, the exit code of the pytest run will be set to 1
if pytest-xdist is detected, it is ensured that the internal tests are not distributed, as this would break the dependency check
after finishing all test runs and displaying the result, “no tests run” is currently displayed on the summary line - this can be safely ignored
Usage of ordering plugins
If you use plugins which change the test order using markers, theses will only be applied in the first test run. The order of the following test runs is solely defined by pytest-find-dependencies. This means that if you use ordering plugins like pytest-order, the dependencies will still be found, if you don’t exclude these tests (which may or may not be wanted). Using pytest-randomly will randomize the first test run and can be used in combination with pytest-find-dependencies without problems.
Contributing
Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.
License
Distributed under the terms of the MIT license, “pytest-find-dependencies” is free and open source software.
Issues
If you encounter any problems or have a feature request, please file an issue along with a detailed description.
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-find-dependencies-0.5.0.tar.gz
.
File metadata
- Download URL: pytest-find-dependencies-0.5.0.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ed29ff831379ad8d6dd9c44ad9e73c746ee9f47c1718dee79566bc4e98c7c4c |
|
MD5 | 58a4921d4f60403defbd1831730af275 |
|
BLAKE2b-256 | 83f6251d622ef3f84767739a44706d1083c9806a2349807cd2df6052b92148ac |
File details
Details for the file pytest_find_dependencies-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: pytest_find_dependencies-0.5.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20e6c6525714b48ff0abf68c95981b3993e233f4ca37d1c5543a0d068e9efa8b |
|
MD5 | 35fafdd1b675211e1d402d1a15c9a101 |
|
BLAKE2b-256 | fb1ba79e859ddd428d6d7f3d2b52ba01a47f0d0c78f819f19d6e1d0a59f5e55d |