Can I Parameterize A Pytest Fixture With Other Fixtures?
I have a python test that uses a fixture for credentials (a tuple of userid and password) def test_something(credentials) (userid, password) = credentials print('Hello {0}, w
Solution 1:
Indirect parametrization is the answer. So, the parameters to the fixtures CAN be other fixtures (by their name/code).
import pytest
all_credentials = {
'staging': ('user1', 'pass1'),
'prod': ('user2', 'pass2'),
}
@pytest.fixturedefcredentials(request):
return all_credentials[request.param]
@pytest.mark.parametrize('credentials', ['staging', 'prod'], indirect=True)deftest_me(credentials):
pass
Technically, you can not only get a dict value by its key, but generate the credentials
result based on request.param
, and this param will be the value passed to the same-name parameter of the test.
If you want other fixtures to be used (probably because of the setup/teardown stages, as this is the only reason to do so):
import pytest
@pytest.fixturedefcredentials(request):
return request.getfuncargvalue(request.param)
@pytest.fixture()defstaging_credentials():
return ("staging_userid", "staging_password")
@pytest.fixture()defproduction_credentials():
return ("prod_userid", "prod_password")
@pytest.mark.parametrize('credentials', ['staging_credentials', 'production_credentials'], indirect=True)deftest_me(credentials):
pass
Here, request.getfuncargvalue(...)
will return a fixture value by the fixture name, dynamically.
Post a Comment for "Can I Parameterize A Pytest Fixture With Other Fixtures?"