pytest-depends
pytest-depends is a pytest plugin that enables defining dependencies between tests. If a test marked as a dependency fails or is skipped, any tests dependent on it will also be skipped. The current version is 1.0.1. The library is in maintenance mode with no recent updates since late 2021.
Common errors
-
ModuleNotFoundError: No module named 'pytest_depends'
cause The `pytest-depends` plugin is not installed in the current Python environment.fixInstall the plugin using pip: `pip install pytest-depends` -
AttributeError: module 'pytest_depends' has no attribute 'depends'
cause The `depends` decorator is typically imported via an alias from the top-level package, not directly as a submodule attribute, leading to an `AttributeError` if attempted.fixImport `pytest_depends` and alias it as `depends`: `import pytest_depends as depends` -
PytestCollectionWarning: Cannot find dependency 'missing_test_name' for '<test_node_id>'
cause The dependency name specified in the `on` list of `@depends.depends()` does not correspond to an actual, discoverable test function name or pytest node ID.fixDouble-check the string value(s) in `on=['your_dependency_name']`. It must be the exact name of the test function (e.g., `test_my_function`) or its full node ID for parameterized tests.
Warnings
- gotcha pytest-depends skips dependent tests if a dependency fails; it does not re-run the dependency or alter its outcome. A 'skipped' test is distinct from a 'failed' test.
- gotcha The pytest-depends project has not been updated in over two years (since November 2021), which may lead to compatibility issues with newer versions of pytest or Python.
- gotcha Dependency names provided in `@depends.depends(on=['...'])` must exactly match the test function names or their full pytest node IDs, including parameters for parameterized tests.
Install
-
pip install pytest-depends
Imports
- depends
from pytest_depends import depends
import pytest_depends as depends
Quickstart
import pytest
import pytest_depends as depends
# This test will pass and its dependent will run
def test_dependency_a_success():
print("\nRunning test_dependency_a_success")
assert True
# This test will fail and its dependent will be skipped
def test_dependency_b_failure():
print("\nRunning test_dependency_b_failure")
assert False
@depends.depends(on=['test_dependency_a_success'])
def test_dependent_on_success_runs():
print("\nRunning test_dependent_on_success_runs")
assert True
@depends.depends(on=['test_dependency_b_failure'])
def test_dependent_on_failure_is_skipped():
print("\nRunning test_dependent_on_failure_is_skipped (expected to be skipped)")
# This assert will not be reached if the test is skipped
assert True
# To run this example, save it as 'test_my_deps.py' and run 'pytest' from your terminal.