pytest-unused-fixtures

raw JSON →
0.3.1 verified Fri May 01 auth: no python

A pytest plugin that lists fixtures defined but not used in a test run. Useful for keeping test suites clean. Current version: 0.3.1. Requires Python >=3.9. Maintained, release cadence is irregular.

pip install pytest-unused-fixtures
error AttributeError: module 'pytest_unused_fixtures' has no attribute 'ignore_unused_fixture'
cause Using an older version (<0.3.0) that does not have the `ignore_unused_fixture` decorator.
fix
Upgrade to 0.3.0 or later: pip install --upgrade pytest-unused-fixtures
error Fixtures imported from third-party packages appear as unused
cause The plugin only captures fixtures defined in the test suite, not fixtures from installed plugins or conftest.py of other packages.
fix
This is expected behavior. Use @ignore_unused_fixture on fixtures you don't want flagged.
gotcha The plugin only reports fixtures that are defined in the same session but never requested. Fixtures from installed packages (e.g., built-in plugins) are not considered unless they are explicitly loaded.
fix Ensure your fixture is defined in the test suite (e.g., conftest.py or test module).
breaking In v0.3.0, the default value of `available_fixtures` changed from `None` to an empty set. If you were relying on `None` in custom code that uses the plugin's internals, your code may break.
fix Update your custom checks to handle an empty set instead of `None`.
deprecated The `--unused-fixtures` CLI option is the only interface. No deprecated options exist as of v0.3.1.
fix No action needed.

Basic usage: define fixtures, run `pytest --unused-fixtures`, and optionally ignore specific ones.

import pytest

# conftest.py or test file
@pytest.fixture
def used_fixture():
    return 1

@pytest.fixture
def unused_fixture():
    return 2

def test_foo(used_fixture):
    assert used_fixture == 1

# Run pytest --unused-fixtures to see 'unused_fixture' listed.
# To ignore a fixture from this check, decorate with @ignore_unused_fixture