Skip to main content

A pytest plugin to list unused fixtures after a test run.

Project description

pytest-unused-fixtures

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.

Installation

$ pip install pytest-unused-fixtures

Options

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

Usage

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.

Ignoring specific fixtures from report

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

Development

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

Thanks

Many thanks to

Changelog

0.2.0 (Mar 15, 2025)

  • Test only on Pytest 8
  • Add --unused-fixtures-context
  • Add --unused-fixtures-fail-when-present
  • Support Python 3.13

0.1.5 (Apr 8, 2024)

  • Support Pytest 8

0.1.4 (Aug 8, 2023)

  • Bump versions, add CI

0.1.3 (Jun 30, 2023)

  • Print line number
  • Support Python 3.9

0.1.2 (Jun 15, 2023)

  • Fix typo, add repository to PyPI
  • Update location handling with respect to showing fixture locations and ignoring paths

0.1.1 (Jun 14, 2023)

  • Fix location handling
  • Add decorator for ignoring fixtures from report

0.1.0 (Jun 14, 2023)

  • First Release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_unused_fixtures-0.2.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

pytest_unused_fixtures-0.2.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_unused_fixtures-0.2.0.tar.gz.

File metadata

  • Download URL: pytest_unused_fixtures-0.2.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytest_unused_fixtures-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c1271648f2bb452a54545df0601e687a1a575990c1eda5d73909548aad6d2363
MD5 7c2fa6e427b2fa4c31c4c129fbf2e2f0
BLAKE2b-256 54668e28c1a69251229e429045cbccced1e0adf96d1be1e21108c104c5f067d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_unused_fixtures-0.2.0.tar.gz:

Publisher: build.yml on mikicz/pytest-unused-fixtures

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_unused_fixtures-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_unused_fixtures-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1cf3d4979a724bc981fc6730d2547e25b103fb8e488c9e6644cbfea11e6a113
MD5 602b608f92457b7bb9ec2c77770c48ee
BLAKE2b-256 8dd8916e39f3b55e6336dce1e1bd4fff39756db4218609f69a07e814fb2c9465

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_unused_fixtures-0.2.0-py3-none-any.whl:

Publisher: build.yml on mikicz/pytest-unused-fixtures

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page