{"record":{"id":"4394c803b199e3b7","repo":"pytest-dev/pytest","slug":"pytest-param-cannot-add-pytest-mark-usefixtures-s","errorCode":null,"errorMessage":"pytest.param cannot add pytest.mark.usefixtures; see https://docs.pytest.org/en/stable/reference/reference.html#pytest-param","messagePattern":"pytest\\.param cannot add pytest\\.mark\\.usefixtures; see https://docs\\.pytest\\.org/en/stable/reference/reference\\.html#pytest-param","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/mark/structures.py","lineNumber":127,"sourceCode":"    \"\"\"\n\n    values: Sequence[object | NotSetType]\n    marks: Collection[MarkDecorator | Mark]\n    id: str | _HiddenParam | None\n\n    @classmethod\n    def param(\n        cls,\n        *values: object,\n        marks: MarkDecorator | Collection[MarkDecorator | Mark] = (),\n        id: str | _HiddenParam | None = None,\n    ) -> ParameterSet:\n        if isinstance(marks, MarkDecorator):\n            marks = (marks,)\n        else:\n            assert isinstance(marks, collections.abc.Collection)\n        if any(i.name == \"usefixtures\" for i in marks):\n            raise ValueError(\n                \"pytest.param cannot add pytest.mark.usefixtures; see \"\n                \"https://docs.pytest.org/en/stable/reference/reference.html#pytest-param\"\n            )\n\n        if id is not None:\n            if not isinstance(id, str) and id is not HIDDEN_PARAM:\n                raise TypeError(\n                    \"Expected id to be a string or a `pytest.HIDDEN_PARAM` sentinel, \"\n                    f\"got {type(id)}: {id!r}\",\n                )\n        return cls(values, marks, id)\n\n    @classmethod\n    def extract_from(\n        cls,\n        parameterset: ParameterSet | Sequence[object] | object,\n        force_tuple: bool = False,\n    ) -> ParameterSet:","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/mark/structures.py#L109-L145","documentation":"A ValueError raised by ParameterSet.param when any of the supplied marks is named 'usefixtures'. pytest forbids attaching usefixtures through pytest.param because fixture application must be resolved at collection time and pytest.param marks are applied per-parameter-row, which would create ambiguous fixture scoping. The message links to the reference docs.","triggerScenarios":"Calling `pytest.param(1, marks=pytest.mark.usefixtures('myfix'))` or passing a tuple of marks containing a usefixtures mark to pytest.param. The check iterates all marks and matches on `.name == 'usefixtures'`.","commonSituations":"Trying to apply a fixture only to a subset of parametrized values, or refactoring a class-level usefixtures into per-param marks. Copying a mark list that inadvertently includes usefixtures.","solutions":["Apply usefixtures at the test function, class, or module level (decorator) instead of via pytest.param.","If you need per-param fixtures, refactor the test so the fixture reads the param value and branches internally.","Use a custom marker and a conftest hook to conditionally request fixtures."],"exampleFix":"# before\n@pytest.mark.parametrize('x', [\n    pytest.param(1, marks=pytest.mark.usefixtures('db')),\n])\ndef test_x(x): ...\n# after\n@pytest.mark.usefixtures('db')\n@pytest.mark.parametrize('x', [1])\ndef test_x(x): ...","handlingStrategy":"validation","validationCode":"def sanitize_marks(marks):\n    return [m for m in marks if getattr(m, 'name', '') != 'usefixtures']","typeGuard":"def contains_usefixtures(marks) -> bool:\n    return any(getattr(m, 'name', '') == 'usefixtures' for m in marks)","tryCatchPattern":null,"preventionTips":["Never put usefixtures in pytest.param marks.","Apply usefixtures at function/class/module level.","Review mark lists before passing to pytest.param."],"tags":["pytest","parametrize","usefixtures","value-error","marks"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}