Skip to content

How to add a fixture to tests under certain conditions at parsing time. #10686

Answered by cladmi
cladmi asked this question in Q&A
Discussion options

You must be logged in to vote

Did not manage to use mark.usefixtures.
The solution that worked for me is the following.

I was careful to not add duplicates but it may not be required.
It is working without the "if name in".

def pytest_generate_tests(metafunc):
    # Detect condition to do it
    usefixture(metafunc, "my_fixture")

def usefixture(metafunc, fixturename)
    if fixturename not in metafunc.fixturenames:
        # Prepend to try executing before normal ones.
        metafunc.fixturenames.insert(0, fixturename)

    fixtureinfo = metafunc.definition._fixtureinfo  # pylint: disable=protected-access
    # This is required to actually have the fixture instantiated
    if fixturename not in fixtureinfo.initialn…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by cladmi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant