pytest-dependency
pytest-dependency is a plugin for the pytest Python testing framework that allows you to manage dependencies between tests. Tests marked as dependent on others will be automatically skipped if any of their dependencies fail or are skipped. The current version is 0.6.1, released in February 2026, and the project maintains an active, albeit irregular, release cadence.
Warnings
- breaking Version 0.6.0 (released Dec 2023) dropped support for Python 2. Ensure your test environment uses Python 3.
- breaking Version 0.5.0 (released Feb 2020) requires `pytest` version 3.7.0 or newer. Older `pytest` versions are incompatible.
- breaking Version 0.3 (released Dec 2017) changed the default naming convention for test methods within test classes. The class name is now prepended to the method name to form the default test name. If you were relying on default names for dependencies of class methods in older versions, you may need to update your `depends` references.
- gotcha `pytest-dependency` is incompatible with `pytest-xdist` when parallelization is enabled, as dependencies assume a sequential execution order. Tests may fail or be skipped incorrectly in parallel runs.
- gotcha Test names in `depends` lists must precisely match the registered test names. For parameterized tests or tests within classes, this often means using the full `pytest` node ID (e.g., `module.py::TestClass::test_method[param]`) or explicitly assigning a `name` via `@pytest.mark.dependency(name='my_test_name')`.
- gotcha Applying the `@pytest.mark.dependency()` marker multiple times to the same test will result in only the *last* invocation being effective. This can silently override previously defined dependencies or custom names.
- gotcha `pytest-dependency` manages skipping based on dependency outcomes but does not reorder tests to ensure dependencies are run first. If pytest's default collection order does not satisfy the dependencies, tests might be skipped unexpectedly because their dependencies haven't been executed yet.
Install
-
pip install pytest-dependency
Imports
- pytest.mark.dependency
import pytest @pytest.mark.dependency()
Quickstart
import pytest
@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail", raises=AssertionError)
def test_a():
assert False
@pytest.mark.dependency()
def test_b():
assert True
@pytest.mark.dependency(depends=["test_a"])
def test_c():
assert True
@pytest.mark.dependency(depends=["test_b"])
def test_d():
assert True
@pytest.mark.dependency(depends=["test_b", "test_c"])
def test_e():
assert True
# To run this, save as `test_deps.py` and run `pytest -rsx test_deps.py`