如何从函数中的pytestfixture获取返回值,这样我就不需要用额外的参数来调用这个函数了?

2024-10-03 06:22:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下固定装置conftest.py返回环境设置字典,如用户、密码等:

@pytest.fixture
def envparams(request):
    env = request.config.getoption("--env")
    return env_params[env]

然后我有模块,比如:

^{pr2}$

以及使用此模块的测试,如:

def test_cje_high_availability(envparams, env_option, script_loc):
    workstation = testinfra.get_host('ssh://'+testinfra_hosts[0])
    if not security.request_master_url(envparams):
        print(test_master+' - is not available\n')
        create_team_master(test_master, envparams, script_loc)

我能从模块函数中去掉envparms参数吗?这样我就可以不用额外的参数来调用它了吗? 比如:

security.request_master_url(envparams)

我只需要在一个会话中设置一次这个设备。我试着用:

@pytest.mark.usefixtures('envparams')
def request_master_url():

但是,我不知道如何从这个fixture返回值。在


Tags: 模块testmasterenvurlpytestrequestdef
1条回答
网友
1楼 · 发布于 2024-10-03 06:22:27

嗯,我已经按照霍夫林的建议做了。在

在中创建了小函数conftest.py公司名称:

def get_env_params():
    env_name = pytest.config.getoption(" env")
    return env_params[env_name]

在需要的地方从我的模块函数调用它。示例函数如下所示:

^{pr2}$

从更多的函数中删除了不必要的fixture,并能够清理大量代码。谢谢!在

相关问题 更多 >