pytest-dev/pytest · error · ValueError
@pytest.fixture is being applied more than once to the same
Error message
@pytest.fixture is being applied more than once to the same function {function.__name__!r} What it means
Raised by `FixtureFunctionMarker.__call__` when the function it receives is already a `FixtureFunctionDefinition`, i.e. `@pytest.fixture` has been stacked twice on the same function. Applying the decorator a second time wraps the already-produced definition object, which pytest rejects because the scope/params/name would be ambiguous.
Source
Thrown at src/_pytest/fixtures.py:1430
@dataclasses.dataclass(frozen=True)
class FixtureFunctionMarker:
scope: ScopeName | Callable[[str, Config], ScopeName]
params: tuple[object, ...] | None
autouse: bool = False
ids: tuple[object | None, ...] | Callable[[Any], object | None] | None = None
name: str | None = None
_ispytest: dataclasses.InitVar[bool] = False
def __post_init__(self, _ispytest: bool) -> None:
check_ispytest(_ispytest)
def __call__(self, function: FixtureFunction) -> FixtureFunctionDefinition:
if inspect.isclass(function):
raise ValueError("class fixtures not supported (maybe in the future)")
if isinstance(function, FixtureFunctionDefinition):
raise ValueError(
f"@pytest.fixture is being applied more than once to the same function {function.__name__!r}"
)
if hasattr(function, "pytestmark"):
fail(
"Marks cannot be applied to fixtures.\n"
"See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function"
)
fixture_definition = FixtureFunctionDefinition(
function=function, fixture_function_marker=self, _ispytest=True
)
name = self.name or function.__name__
if name == "request":
location = getlocation(function)
fail(
f"'request' is a reserved word for fixtures, use another name:\n {location}",View on GitHub (pinned to 98b357f69e)
Solutions
- Use exactly one `@pytest.fixture(...)` decorator per function with all options in a single call.
- If a helper adds fixture behavior, have it return a plain function and decorate once at the definition site.
- Remove the redundant decorator.
Example fix
// before
@pytest.fixture(scope="session")
@pytest.fixture
def x():
yield 1
// after
@pytest.fixture(scope="session")
def x():
yield 1 Defensive patterns
Strategy: validation
Validate before calling
def ensure_single_fixture_decorator(fn):
if isinstance(fn, FixtureFunctionDefinition):
raise ValueError(f"{fn.__name__!r} already decorated with @pytest.fixture")
return fn Type guard
def is_already_fixture(fn) -> bool:
from _pytest.fixtures import FixtureFunctionDefinition
return isinstance(fn, FixtureFunctionDefinition) Prevention
- Apply @pytest.fixture exactly once per function.
- Put all fixture options in a single decorator call.
- Avoid helper decorators that re-apply pytest.fixture.
When it happens
Trigger: Stacking decorators: `@pytest.fixture(scope="session")\n@pytest.fixture\ndef x(): ...`, or a helper that reapplies `@pytest.fixture` to an already-decorated fixture.
Common situations: Copy-paste of decorators. A custom decorator that internally calls `pytest.fixture` applied to a function already decorated. Migrating old code that used `@pytest.yield_fixture` plus `@pytest.fixture`.
Related errors
- {request.fixturename} did not yield a value
- class fixtures not supported (maybe in the future)
- function not available in {self.scope}-scoped context
- cls not available in {self.scope}-scoped context
- module not available in {self.scope}-scoped context
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/f7b5755152b533cb.json.
Report an issue: GitHub.