pytest-find-dependencies
A pytest plugin designed to automatically identify dependencies between tests. It achieves this by running tests in various sequences (forward and backward) and employing a binary search algorithm to pinpoint which tests rely on others. This can help uncover hidden state-based dependencies that lead to flaky tests. The current version is 0.6.0 and it is actively maintained.
Warnings
- gotcha The plugin's author suggests considering `detect-test-pollution` as an alternative, noting its superior documentation and greater popularity.
- gotcha When `pytest-xdist` (a plugin for parallel test execution) is detected, `pytest-find-dependencies` ensures that its internal dependency-finding test runs are not distributed. This is to prevent breaking the dependency check mechanism.
- gotcha By default, the dependency analysis run returns an exit code of 1 if *any* dependency is found, even if all tests pass. If you want the exit code to reflect actual test failures, use the `--fail-on-failed-tests` option.
- gotcha Dependencies that stem from a permanent change in the test environment (e.g., a resource being permanently altered) will only be found if the test causing the change runs *before* the dependent test during the plugin's analysis.
- gotcha If you use other plugins that change test order (e.g., `pytest-order`), their ordering rules will only apply to the *first* test run during dependency analysis. Subsequent runs by `pytest-find-dependencies` for dependency isolation will define their own order.
Install
-
pip install pytest-find-dependencies
Imports
- pytest-find-dependencies
This plugin is primarily activated via the pytest command-line interface using the `--find-dependencies` option. Direct Python imports into user test files for its core functionality are not typical.
Quickstart
import pytest
# Simulate a shared state that might be modified by one test and read by another
_shared_state = {'value': 0}
def test_setup_value():
"""This test sets a value in the shared state."""
_shared_state['value'] = 10
assert True
def test_use_value():
"""This test depends on the value being set by test_setup_value."""
assert _shared_state['value'] == 10
def test_independent():
"""An independent test with no dependencies."""
assert 1 + 1 == 2
# To run this example, save it as `test_dependencies.py` and execute:
# pytest --find-dependencies test_dependencies.py
# The output should indicate a dependency found between 'test_use_value' and 'test_setup_value'.