pytest-deadfixtures
pytest-deadfixtures is a simple plugin for pytest that helps identify unused or duplicated fixtures within your test suite. It aids in improving code quality by highlighting fixtures that are no longer needed or could be refactored. The current version is 3.1.0, and the library appears to be actively maintained with a consistent release cadence.
Warnings
- breaking Version 3.0.0 dropped support for Python 3.5. Ensure your environment uses Python 3.6 or newer.
- gotcha The `--dead-fixtures` option *does not run your tests*. It is sensitive to errors in the pytest collection step. For CI/CD, it's recommended to run `pytest --dead-fixtures` *after* a successful default `pytest` run.
- gotcha When using `--dup-fixtures` to find duplicated fixtures, the plugin relies on comparing fixture *return values*. Fixtures that return `None` or other falsy values will be skipped from this analysis. This feature should be used as a hint for manual verification.
- gotcha By default, the plugin returns a non-zero exit code if unused fixtures are detected (since version 2.0.0). This will cause CI pipelines to fail if any unused fixtures are found.
- gotcha pytest-deadfixtures primarily relies on static analysis to detect unused fixtures. It might not correctly identify fixtures used dynamically (e.g., via `request.getfixturevalue()` or other advanced runtime mechanisms).
Install
-
pip install pytest-deadfixtures
Imports
- deadfixtures_ignore
from pytest_deadfixtures import deadfixtures_ignore
Quickstart
import pytest
from pytest_deadfixtures import deadfixtures_ignore
@pytest.fixture
def used_fixture():
return "I am used"
@pytest.fixture
def unused_fixture():
"""This fixture is intentionally unused for demonstration."""
return "I am not used"
@pytest.fixture
@deadfixtures_ignore
def ignored_fixture():
"""This fixture will be explicitly ignored by the plugin."""
return "I am ignored"
def test_example(used_fixture):
assert used_fixture == "I am used"
# To run this example and see the unused fixture report, save it as a Python file (e.g., test_fixtures.py)
# and execute from your terminal:
# pytest --dead-fixtures --show-ignored-fixtures -v test_fixtures.py