A pytest plugin to list unused fixtures after a test run.
The plugin watches all fixtures which are used in a test run, and then compares it to all the fixtures available in the same test collection. It then prints out all the fixtures which were not used, groupped by where they were defined.
Running the tests is required to accurately record which fixtures are used, as pytest provides ways of dynamically requesting fixtures, and pure static analysis will not catch those.
$ pip install pytest-unused-fixtures
Option Name | Type | Description |
---|---|---|
--unused-fixtures |
switch | Enable plugin |
--unused-fixtures-ignore-path |
string* | Ignore paths for consideration of unused fixtures |
--unused-fixtures-context |
array<string> | Only consider fixtures missing if defined in these directories |
--unused-fixtures-fail-when-present |
switch | Fail pytest session if unused fixtures are present |
After installing the package, the plugin is enabled by adding the switch --unused-fixtures
.
Paths of fixtures can be ignored with one or multiple --unused-fixtures-ignore-path
arguments. For example --unused-fixtures-ignore-path=venv
will ignore all fixtures defined in the venv
folder.
Alternatively, you can limit the scope in which the plugin looks for unused fixtures to a specific directory or directories. For example:
Limit scope to one directory
This example will only display unused fixtures that were defined in the tests
folder
pytest tests --unused-fixtures --unused-fixtures-context tests
Limit scope to multiple directories
This example will only display unused fixtures that were defined in the directory1
and directory2/sub-directory
folders
pytest tests --unused-fixtures --unused-fixtures-context directory1 directory2/sub-directory
Fail test session
By default, when you use the --unused-fixtures
switch, the plugin will exit with the same exit code pytest
would have used if running without the plugin. Add the switch --unused-fixtures-fail-when-present
and the
pytest session will return a non-zero exit code if there are unused fixtures.
Sometimes there will be fixture which are unused on purpose, for example when used in tests which are skipped by default. A decorator is provided for ignoring fixtures from the unused report. See the example for usage:
import pytest
from pytest_unused_fixtures import ignore_unused_fixture
@pytest.fixture
@ignore_unused_fixture
def ignored_fixture():
pass
Poetry (dependencies) and pre-commit (coding standards) are required for development. There are some tests, obviously written in pytest.
$ poetry install
$ pre-commit install
$ pytest tests
Many thanks to
- pytest-deadfixtures for inspiration for this project
- pytest-durations for inspirations in parts of the implementation
- Test only on Pytest 8
- Add
--unused-fixtures-context
- Add
--unused-fixtures-fail-when-present
- Support Python 3.13
- Support Pytest 8
- Bump versions, add CI
- Print line number
- Support Python 3.9
- Fix typo, add repository to PyPI
- Update location handling with respect to showing fixture locations and ignoring paths
- Fix location handling
- Add decorator for ignoring fixtures from report
- First Release