我可以使用其他灯具参数化pytest灯具吗? [英] Can I parameterize a pytest fixture with other fixtures?
问题描述
我有一个python测试,该测试使用用于凭证的夹具(用户ID和密码的元组)
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}, welcome to my test".format(userid))
并且我有用于证书的pytest固定装置:
and I have pytest fixture for credentials:
@pytest.fixture()
def credentials():
return ("my_userid", "my_password")
效果很好
现在,我想将其扩展为多个凭证(例如登台和生产),以便我的测试将运行两次(每个登台和生产一次).
Now I want to extend this for multiple credentials (say staging and production) so that my test will run twice (once each for staging and production).
我以为参数化是答案,但是似乎我无法使用灯具进行参数化.
I thought that parameterization was the answer, but it seems I can't parameterize with fixtures.
我想做这样的事情:
@pytest.fixture(params=[staging_credentials, production_credentials])
def credentials(request):
return request.param
其中staging_credentials和production_credentials都是固定装置:
where staging_credentials and production_credentials are all fixtures:
@pytest.fixture()
def staging_credentials():
return ("staging_userid", "staging_password")
@pytest.fixture()
def production_credentials():
return ("prod_userid", "prod_password")
但是显然夹具的参数不能是其他夹具.
but apparently parameters to the fixture can't be other fixtures.
关于如何优雅地处理此问题的任何建议?我看过 https://docs.pytest.org/en/latest/proposal/parametrize_with_fixtures.html ,但这似乎太过残酷了.
Any suggestions on how to elegantly handle this? I've looked at https://docs.pytest.org/en/latest/proposals/parametrize_with_fixtures.html but that seems a but too brute force.
谢谢! 史蒂夫
推荐答案
间接参数化就是答案.因此,灯具的参数可以是其他灯具(按其名称/代码).
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.fixture
def credentials(request):
return all_credentials[request.param]
@pytest.mark.parametrize('credentials', ['staging', 'prod'], indirect=True)
def test_me(credentials):
pass
从技术上讲,您不仅可以通过其键获取dict值,还可以基于request.param
生成credentials
结果,并且该参数将成为传递给测试的同名参数的值.
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.fixture
def credentials(request):
return request.getfuncargvalue(request.param)
@pytest.fixture()
def staging_credentials():
return ("staging_userid", "staging_password")
@pytest.fixture()
def production_credentials():
return ("prod_userid", "prod_password")
@pytest.mark.parametrize('credentials', ['staging_credentials', 'production_credentials'], indirect=True)
def test_me(credentials):
pass
此处,request.getfuncargvalue(...)
将根据灯具名称动态返回灯具值.
Here, request.getfuncargvalue(...)
will return a fixture value by the fixture name, dynamically.
这篇关于我可以使用其他灯具参数化pytest灯具吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!