Skip to content

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

@amitw-di

Description

@amitw-di

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: #12205 (reply in thread)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytopic: parametrizerelated to @pytest.mark.parametrize

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions