Skip to content

pytest-fixture-autouse (RUF076)

Removed (since 0.15.20) · Related issues · View source

Warning: This rule has been removed and its documentation is only available for historical reasons.

Removed

This rule has been removed because it is highly opinionated and may encourage unidiomatic pytest usage. It may be reintroduced in the future under a different category but was not a good fit for the RUF category.

What it does

Checks for pytest fixtures that set the parameter autouse=True in the decorator constructor.

Why is this bad?

Autouse fixtures are run implicitly, which can make test behavior hard to reason about in general, but especially when defined in conftest.py files. Autouse fixtures in conftest.py files are automatically run for all tests in the directory structure, which can introduce hidden side effects, make test suites slower, and make debugging difficult.

Instead, prefer to explicitly request/inject fixtures in tests, test classes, or other fixtures that need them by declaring them in the function parameters.

Example

import pytest


@pytest.fixture(autouse=True)
def my_fixture(): ...

Use instead:

import pytest


@pytest.fixture()
def my_fixture(): ...


def test_foo(my_fixture): ...

Note

This is a pedantic rule that restricts a valid pytest pattern. If you choose to enable it, you may want to ignore it outside of conftest.py files, as autouse fixtures are most problematic when defined globally.

You can do this by configuring lint.per-file-ignores:

[tool.ruff.lint.per-file-ignores]
"!**/conftest.py" = ["RUF076"]

References