Package: pytest-find-dependencies
A pytest plugin to find dependencies between tests.Thispytestplugin was generated withCookiecutteralong with@hackebrot’scookiecutter-pytest-plugintemplate.SummaryTests shall generally not depend on each other. To ensure this, plugins
likepytest-randomlyorpytest-reverseare 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 aspytest-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.InstallationYou can installpytest-find-dependenciesviapipfromPyPI:$ pip install pytest-find-dependenciesUsageIf 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:============================== Results ===============================
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-firstallows you to reverse the sequence of the
first two test runs.The option--markers-to-ignoreallows 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 includedependency(from thepytest-dependencyplugin),order(frompytest-order) ordepends(frompytest-depends). To ignore all tests with adependencymarker, you can use:python -m pytest --find-dependencies --markers-to-ignore=dependencyNote that in this case you also won’t find other tests depending on the
ordered markers.Notescommand line options given in the test are passed to all test runs
in dependency find modeif any dependent tests are found, the exit code of the pytest run will be
set to 1ifpytest-xdistis detected, it is ensured that the internal tests
are not distributed, as this would break the dependency checkif no dependent tests are found, the test will run twice as long as without
the option (because each test will be executed twice), with one dependent
test it will take about three times as long as the original tests, and with
each found dependent test it will take more time (e.g. more repeated tests)Usage of ordering pluginsIf 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 bypytest-find-dependencies. This means that if you use
ordering plugins likepytest-order, the dependencies will still be
found, if you don’t exclude these tests (which may or may not be wanted).
Usingpytest-randomlywill randomize the first test run and can be used
in combination withpytest-find-dependencieswithout problems.ContributingContributions are very welcome. Tests can be run withtox, please ensure
the coverage at least stays the same before you submit a pull request.LicenseDistributed under the terms of theMITlicense,
“pytest-find-dependencies” is free and open source software.IssuesIf you encounter any problems or have a feature request, pleasefile an issuealong with a detailed description.