pytest-dev/pytest · error · ValueError

pytest.param cannot add pytest.mark.usefixtures; see https:/

Error message

pytest.param cannot add pytest.mark.usefixtures; see https://docs.pytest.org/en/stable/reference/reference.html#pytest-param

What it means

`pytest.param(...)` explicitly forbids attaching `pytest.mark.usefixtures` via its `marks=` argument — usefixtures may only be applied to the test function itself. `ParameterSet.param` scans the provided marks and raises ValueError if any has `name == 'usefixtures'`. This is a deliberate API restriction.

Source

Thrown at src/_pytest/mark/structures.py:127

    """

    values: Sequence[object | NotSetType]
    marks: Collection[MarkDecorator | Mark]
    id: str | _HiddenParam | None

    @classmethod
    def param(
        cls,
        *values: object,
        marks: MarkDecorator | Collection[MarkDecorator | Mark] = (),
        id: str | _HiddenParam | None = None,
    ) -> ParameterSet:
        if isinstance(marks, MarkDecorator):
            marks = (marks,)
        else:
            assert isinstance(marks, collections.abc.Collection)
        if any(i.name == "usefixtures" for i in marks):
            raise ValueError(
                "pytest.param cannot add pytest.mark.usefixtures; see "
                "https://docs.pytest.org/en/stable/reference/reference.html#pytest-param"
            )

        if id is not None:
            if not isinstance(id, str) and id is not HIDDEN_PARAM:
                raise TypeError(
                    "Expected id to be a string or a `pytest.HIDDEN_PARAM` sentinel, "
                    f"got {type(id)}: {id!r}",
                )
        return cls(values, marks, id)

    @classmethod
    def extract_from(
        cls,
        parameterset: ParameterSet | Sequence[object] | object,
        force_tuple: bool = False,
    ) -> ParameterSet:

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Apply `@pytest.mark.usefixtures('db')` to the whole test function instead of to individual params.
  2. If only some params need the fixture, split into two test functions or parametrize the fixture itself with `indirect=True`.
  3. Use `params=` of the fixture to vary behavior instead of usefixtures on params.

Example fix

# before
@pytest.mark.parametrize('x', [pytest.param(1, marks=pytest.mark.usefixtures('db'))])
def test_x(x): ...
# after
@pytest.mark.usefixtures('db')
@pytest.mark.parametrize('x', [1])
def test_x(x): ...
Defensive patterns

Strategy: validation

Validate before calling

def has_no_usefixtures(marks) -> bool:
    return all(getattr(m, 'name', getattr(getattr(m, 'mark', None), 'name', None)) != 'usefixtures' for m in marks)

Prevention

When it happens

Trigger: `pytest.param(1, 2, marks=pytest.mark.usefixtures('db'))` inside a parametrize values list.

Common situations: Trying to make only some parameter combinations use a fixture; copy-pasting a usefixtures mark into parametrize.

Related errors


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/4394c803b199e3b7.json. Report an issue: GitHub.