{"id":"d080482d14e51f65","repo":"pytest-dev/pytest","slug":"class-fixtures-not-supported-maybe-in-the-future","errorCode":null,"errorMessage":"class fixtures not supported (maybe in the future)","messagePattern":"class fixtures not supported \\(maybe in the future\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/fixtures.py","lineNumber":1427,"sourceCode":"\n\n@final\n@dataclasses.dataclass(frozen=True)\nclass FixtureFunctionMarker:\n    scope: ScopeName | Callable[[str, Config], ScopeName]\n    params: tuple[object, ...] | None\n    autouse: bool = False\n    ids: tuple[object | None, ...] | Callable[[Any], object | None] | None = None\n    name: str | None = None\n\n    _ispytest: dataclasses.InitVar[bool] = False\n\n    def __post_init__(self, _ispytest: bool) -> None:\n        check_ispytest(_ispytest)\n\n    def __call__(self, function: FixtureFunction) -> FixtureFunctionDefinition:\n        if inspect.isclass(function):\n            raise ValueError(\"class fixtures not supported (maybe in the future)\")\n\n        if isinstance(function, FixtureFunctionDefinition):\n            raise ValueError(\n                f\"@pytest.fixture is being applied more than once to the same function {function.__name__!r}\"\n            )\n\n        if hasattr(function, \"pytestmark\"):\n            fail(\n                \"Marks cannot be applied to fixtures.\\n\"\n                \"See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function\"\n            )\n\n        fixture_definition = FixtureFunctionDefinition(\n            function=function, fixture_function_marker=self, _ispytest=True\n        )\n\n        name = self.name or function.__name__\n        if name == \"request\":","sourceCodeStart":1409,"sourceCodeEnd":1445,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/fixtures.py#L1409-L1445","documentation":"Raised by `FixtureFunctionMarker.__call__` when `@pytest.fixture` is applied to a class rather than a function (`inspect.isclass(function)` is true). pytest fixtures must be functions (or generator functions); class-based fixtures are not supported, hence the \"maybe in the future\" note.","triggerScenarios":"Decorating a class: `@pytest.fixture\\nclass DB: ...`, or accidentally applying the decorator to a class-based test helper that should not be a fixture.","commonSituations":"Coming from frameworks with class-based fixtures (e.g. unittest setUpClass patterns) and assuming pytest supports them. Misreading docs that show factory fixtures returning class instances.","solutions":["Convert the class into a fixture function that returns an instance: `@pytest.fixture\\ndef db():\\n    return DB()`.","If the class is meant to group helpers, leave it undecorated and instantiate it in a fixture.","Use a factory fixture pattern: `@pytest.fixture\\ndef make_db():\\n    return DB`."],"exampleFix":"// before\n@pytest.fixture\nclass DB:\n    def __init__(self): self.x = 1\n// after\nclass DB:\n    def __init__(self): self.x = 1\n\n@pytest.fixture\ndef db():\n    return DB()","handlingStrategy":"validation","validationCode":"import inspect\n\ndef fixture_or_factory(obj):\n    if inspect.isclass(obj):\n        raise TypeError(\"class fixtures not supported; wrap in a function fixture\")\n    return obj","typeGuard":"def is_fixture_function(obj) -> bool:\n    import inspect\n    return inspect.isfunction(obj) or inspect.isgeneratorfunction(obj)","tryCatchPattern":null,"preventionTips":["Only apply @pytest.fixture to functions/generators.","Return class instances from a function fixture."],"tags":["pytest","fixtures","class-fixture","valueerror"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}