Multiple param values in fixture #10899
|
Hi all, I've noticed a not very well documented behavior in parametrizing fixtures. A fixture with params errors out in the discovery if the params have multiple values. I believe it's because a single name fixture cannot bind different parameter values, but I didn't find anything about it in the docs, neither the error is clear: import pytest
@pytest.fixture(params=[pytest.param(i, 5) for i in range(5)])
def my_fixture(request):
return request.param
def test_foo(my_fixture):
assert my_fixture in {1, 2, 3, 4, 5} |
Replies: 1 comment 2 replies
|
If you want @pytest.fixture(params=[pytest.param((i, 5)) for i in range(5)])
def my_fixture(request):
return request.paramYou can also omit @pytest.fixture(params=[(i, 5) for i in range(5)])
def my_fixture(request):
return request.paramThen: def test_foo(my_fixture):
i, value = my_fixtureIf the pytest.param(i, id="5")Please mark this answer as accepted if it helped, thank you. |
Thanks for the additional context. A parametrized fixture only has one parameter slot: the fixture itself. Each entry in
paramsbecomes the single value exposed asrequest.param.So this does not work:
@pytest.fixture(params=[pytest.param(i, 5) for i in range(5)])because
pytest.param(i, 5)creates a parameter set containing two values, while the fixture parametrization expects one.If the intended fixture value is the pair
(i, 5), make that pair the single parameter:or simply: