pytest-dev/pytest · error · ValueError

class fixtures not supported (maybe in the future)

Error message

class fixtures not supported (maybe in the future)

What it means

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.

Source

Thrown at src/_pytest/fixtures.py:1427


@final
@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":

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Convert the class into a fixture function that returns an instance: `@pytest.fixture\ndef db():\n return DB()`.
  2. If the class is meant to group helpers, leave it undecorated and instantiate it in a fixture.
  3. Use a factory fixture pattern: `@pytest.fixture\ndef make_db():\n return DB`.

Example fix

// before
@pytest.fixture
class DB:
    def __init__(self): self.x = 1
// after
class DB:
    def __init__(self): self.x = 1

@pytest.fixture
def db():
    return DB()
Defensive patterns

Strategy: validation

Validate before calling

import inspect

def fixture_or_factory(obj):
    if inspect.isclass(obj):
        raise TypeError("class fixtures not supported; wrap in a function fixture")
    return obj

Type guard

def is_fixture_function(obj) -> bool:
    import inspect
    return inspect.isfunction(obj) or inspect.isgeneratorfunction(obj)

Prevention

When it happens

Trigger: Decorating a class: `@pytest.fixture\nclass DB: ...`, or accidentally applying the decorator to a class-based test helper that should not be a fixture.

Common situations: 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.

Related errors


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