I have a fixture that creates a list of items during tests. I want to have another fixture which is parametrized with values generated by the first one.
Example code
import random
import pytest
@pytest.fixture
def values():
return [random.randint(0, 100) for _ in range(10)]
@pytest.fixture
def value(request):
return request.param
@pytest.mark.parametrize("value", params=values):
def test_function(value):
assert value > 0
The problem with above code is that values is a function and not a list. I did quite a lot of digging but didnt find any way to unpack fixture to parametrize another with it.
Im aware that i can pass values fixture and iterate over it in tests, but that not a good solution since i want to see which values cause test to fail.
Im also open to alternative solutions, for example if it is possible to run subtests from started test.
pytest-lazy-fixture, you can try that out if you want. However, in your particular example, thevaluesfixture has no args and can be easily used as a plain function, so you can just mark the test with@pytest.mark.parametrize('value', values()). - hoeflingvaluesas a module-level variable - does it really need to be a fixture? - Tom Daltonvaluesfixture uses some other fixtures defined above it. To make it module-level variable i would have to make all fixtures that it depends on into variables as well. As forpytest-lazy-fixture, it allows to use fixtures as params but not as source for params - you cant doparametrize("x", params=lazy_fixture(values))- festinuz