Skip to content Skip to sidebar Skip to footer

Passing Pytest Fixture In Parametrize

I'm getting following error by passing my fixture defined in conftest.py in @pytest.mark.parametrize: pytest --alist='0220,0221' test_1.py -v -s NameError: name 'alist' is not defi

Solution 1:

As mentioned in the comment, you cannot directly pass a fixture to a mark.parametrize decorator, because the decorator is evaluated at load time. You can do the parametrization at run time instead by implementing pytest_generate_tests:

import pytest

@pytest.hookimpldefpytest_generate_tests(metafunc):
    if"alist"in metafunc.fixturenames:
        values = metafunc.config.option.alist
        if value isnotNone:
            metafunc.parametrize("alist", value.split(","))

deftest_ra_start_time(alist):
    for channel in alist:
        print(channel)

deftest_something_else():
    # will not be parametrizedpass

The parametrization is done based on the presence of the alist parameter in the test function. For parametrization to work, this parameter is needed (otherwise you would get an error because of the missing argument).

Post a Comment for "Passing Pytest Fixture In Parametrize"