Im trying to use list of one fixture values as parameters into another. Here is my setup:
import pytest
d = {
"first": [1, 2, 3],
"second": [4, 5, 6]
}
@pytest.fixture(params=['first', 'second'])
def foo(request):
return d.get(request.param)
@pytest.fixture(params=[pytest.lazy_fixture('foo')])
def bar(request):
return request.param
def test_1(bar):
pass
The problem is bar()
always get full list as request.param
([1, 2, 3] not values of the list. If in params
of bar()
fixture send data directly, e.g:
@pytest.fixture(params=[1, 2, 3])
def bar(request):
return request.param
def test_1(bar):
pass
Then argument request will work properly (test starts three times). The same situation, if I pass arguments to params
not directly, but from any method without fixture decorator, i.e:
def some():
return [1, 2, 3]
@pytest.fixture(params=some())
def more(request):
return request.param
def test_2(more):
logging.error(more)
pass
So, my question is it possible to obtain data from list one by one and then use it in my test? I've try to 'unparse' list:
@pytest.fixture(params=[i for i in i pytest.lazy_fixture('foo')])
def bar(request):
return request.param
But in this case I get TypeError: 'LazyFixture' object is not iterable
See this answer to a very similar question: by design, fixture values can not be used as lists of parameters to parametrize tests. Indeed parameters are resolved at pytest collection phase, while, fixtures are resolved later, during pytest nodes execution.
The best that you can do with lazy_fixture
or with pytest_cases.fixture_ref
is to use a single fixture value as a parameter. With lazy_fixture
you have limitations (if I remember correctly, the fixture cannot be parametrized) while with pytest_cases
you can do pretty much anything (using the fixture_ref
in a parameter tuple, using several fixture_ref
s each with different parametrization, etc.). I'm the author of pytest_cases
by the way ;)
See also this thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With