pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Adding parameterized fixture in pytest_generate_test is not executing the fixture's code, even if it is marked as "indirect"

Open amitw-di opened this issue 1 year ago • 0 comments

Using pytest 8.1.1, I'm trying to add a parameterized fixture to a test during pytest_generate_tests. While it's possible to do so when the test is expecting a parameter with the fixture name, trying to add a "usefixtures" mark in order to invoke an undeclared fixture, does not.

Minimal reproduction:

import pytest


def pytest_generate_tests(metafunc):
    metafunc.definition.own_markers.append(pytest.mark.usefixtures('myfix'))
    # metafunc.fixturenames.append('myfix')
    metafunc.parametrize('myfix', ['myfix-1', 'myfix-2', 'myfix-3'], indirect=True)

@pytest.fixture()
def myfix(request):
    raise Exception(request.param)

def test_me():
    pass

In this form, the code results in collection error, claiming In test_me: function uses no fixture 'myfix' If the commented out line is uncommented, which, according to @RonnyPfannschmidt shouldn't be done), the test is running and looks as if it's parameterized (even without the "usefixtures" part) , but the fixture's code is not invoked.

Other attempts I've tried are described here: https://github.com/pytest-dev/pytest/discussions/12205#discussioncomment-9083200

There should be a way to conditionally add a fixture to a test, and according to some older discussions, it has been the case in the past

amitw-di avatar Apr 15 '24 07:04 amitw-di