Skip to main content

PyPI version Python Development Status Maintenance PyPI License


pytest-depends-on

An advanced pytest plugin designed for Python projects, offering robust dependency management utilities to enhance the testing workflow.
It allows tests to be skipped based on the execution status of other tests, ensuring that dependent tests do not run if their prerequisites fail or are skipped, resulting in a cleaner and more efficient testing experience.


🚀 Features

  • ✅ depends-on: A powerful marker to declare test dependencies. If a parent test fails or hasn't run, the dependent test is automatically skipped.
    • Marker: @pytest.mark.depends_on
    • Arguments:
      • tests (list): A list of parent test names (node IDs) that the current test depends on.
      • status (optional): The expected status of the parent test. Defaults to PASSED. The dependent test will skip if the parent status does not match this value.
      • allowed_not_run (optional): Boolean. If True, the test will not skip if the parent test has not run yet. Defaults to False (skips if parent is missing).
      • match_params (optional): List of parametrize argument names (e.g. ["chip"]). The parent name may omit its [...] suffix, and only the parent instance(s) whose values for those arguments equal the child's are used. Can also be set per entry when tests items are dicts.

  • ✅ Automatic Status Tracking: Automatically tracks the result (passed, failed, skipped, xfailed, xpassed) of every test during the call phase to resolve dependencies dynamically.
  • ✅ Opt-in activation: The plugin is inactive by default. Pass --depends-on to enable dependency tracking and skip behaviour.
  • ✅ Automatic test reordering: Pass --depends-on-reorder to reorder the collected test suite so parent tests always execute before their dependents — no manual ordering required.

📦 Installation

pip install pytest-depends-on

🔧 Usage

1. Enable the plugin

The plugin is opt-in. Add the flags to your pytest.ini (recommended) or pass them directly on the command line:

[pytest]
addopts =
    --depends-on          # enable dependency tracking and skip behaviour
    --depends-on-reorder  # reorder tests so parents always run first (requires --depends-on)

markers =
    depends_on: mark test as dependent on another test

Or run ad-hoc:

pytest --depends-on --depends-on-reorder

Without --depends-on, all depends_on markers are ignored and every test runs normally.

2. Implement in your tests

Use the marker decorator on your test functions.

Basic Dependency: test_child will only run if test_parent passes.

import pytest

def test_parent():
    assert True

@pytest.mark.depends_on(tests=["test_parent"])
def test_child():
    assert True

Custom Status Dependency: test_child will run only if test_parent fails (useful for testing error handling or recovery).

import pytest

from pytest_depends_on.consts.status import Status

@pytest.mark.depends_on(tests=["test_parent"], status=Status.FAILED)
def test_child():
    pass

Soft Dependency: test_child will run even if test_parent hasn't executed yet (e.g., due to ordering).

import pytest

@pytest.mark.depends_on(tests=["test_parent"], allowed_not_run=True)
def test_child():
    pass

Parametrized Dependency (match_params): Each test_boot[chip] depends on the test_flash instance with the same chip. Without match_params, tests=["test_flash"] would not match any parametrized instance.

import pytest

@pytest.mark.parametrize("chip", ["a", "b"])
def test_flash(chip):
    ...

@pytest.mark.parametrize("chip", ["a", "b"])
@pytest.mark.depends_on(tests=["test_flash"], match_params=["chip"])
def test_boot(chip):
    ...  # test_boot[b] is skipped if test_flash[b] did not pass

Several names can be combined (match_params=["chip", "board"]); all must match. A name the child does not parametrize raises a UsageError.


🤝 Contributing

If you have a helpful tool, pattern, or improvement to suggest: Fork the repo
Create a new branch
Submit a pull request
I welcome additions that promote clean, productive, and maintainable development.


🙏 Thanks

Thanks for exploring this repository!
Happy coding!

Metadata

Release files for pytest-depends-on 2.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-depends-on 2.3.0
File Size Uploaded Provenance
pytest_depends_on-2.3.0.tar.gz 10.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-depends-on 2.3.0
File Interpreter ABI Platform
pytest_depends_on-2.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.1 kB

Release files / pytest_depends_on-2.3.0.tar.gz

Download URL pytest_depends_on-2.3.0.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
92c1a6c7319145b5bb63b38150da43948dc321a79e133e89628e9f20a12c5aa9
BLAKE2b-256 checksum
How to use checksums
fdae4374a83ca7d8b512d85e0c512cf4c41b7840c9a3f037a53c3323b7740a5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pytest_depends_on-2.3.0-py3-none-any.whl

Download URL pytest_depends_on-2.3.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17f547ac86355c1bbb6cca632647bab2612c36ce08c4f14a9107454a869714b7
BLAKE2b-256 checksum
How to use checksums
7df86c139ff3b7084c04d0b370de08e0183fdaf3ca3c96a8002aa066fef2b133
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for pytest-depends-on 2.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page