I want to pass a pytest.fixture
function to another fixture function's param argument. Something like this:
@pytest.fixture()
def foo():
return "foo"
@pytest.fixture()
def boo(foo):
return foo + "boo"
@pytest.fixture()
def bar(foo):
return foo + "bar"
@pytest.fixture(params=[boo, bar], ids=["boo_fixture", "bar_fixture"])
def final_fixture(request):
return request.param
def _test(final_fixture):
assert final_fixture.startswith('foo')
The request.param
in final_fixture
returns the function object for that param, instead of the return value of the fixtures(boo
and bar
)
<function boo at 0x7f2bfceg41f0>
<function bar at 0x7f2bfceg41f1>
So, how can I make the final_fixture
function return the actual return values for each fixture param
?
I found a solution that worked for me, thanks to this answer by Will.
It seems like you can just use request.getfixturevalue
to get the return value of the fixture instead of the function object. Here is the working code for this:
@pytest.fixture()
def foo():
return "foo"
@pytest.fixture()
def boo(foo):
return foo + "boo"
@pytest.fixture()
def bar(foo):
return foo + "bar"
@pytest.fixture(params=['boo', 'bar'], ids=["boo_fixture", "bar_fixture"])
def final_fixture(request):
return request.getfixturevalue(request.param)
def _test(final_fixture):
assert final_fixture.startswith('foo')
Here is the related doc: http://doc.pytest.org/en/latest/builtin.html#_pytest.fixtures.FixtureRequest.getfixturevalue