Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
amitw-di opened this issue Apr 15, 2024 · 0 comments

Comments

@amitw-di
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant