我在conftest.py中有以下fixture,它返回一个环境设置字典,如用户、密码等:
@pytest.fixture
def envparams(request):
env = request.config.getoption("--env")
return env_params[env]然后我有像这样的模块:
def request_master_url(envparams):
cje_master_url = envparams['url']+'/'+test_master
cje_user = envparams['user']
cje_pass = envparams['password']
local = testinfra.get_host("ssh://localhost")
results = local.command(
'curl -L -I --user '+cje_user+':'
+ cje_pass+' '+cje_master_url+'| grep HTTP\
|tail -1').stdout
if '200 OK' in results:
return True
else:
return False以及使用此模块的测试,如:
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)我能不能从模块函数中去掉envparams参数,这样我就可以在没有额外参数的情况下调用它了?像这样:
security.request_master_url(envparams)我只需要在一个会话中设置这个夹具一次。我试着使用:
@pytest.mark.usefixtures('envparams')
def request_master_url():但是,我不确定如何从这个fixture中获取返回值。
发布于 2018-12-06 23:56:13
好吧,我已经按照hoefling的建议做了。
在conftest.py中创建了小函数:
def get_env_params():
env_name = pytest.config.getoption("--env")
return env_params[env_name]并在需要的地方从我的模块函数调用它。示例函数如下所示:
def request_master_url(team_id):
envparams = get_env_params()
cje_master_url = envparams['url']+'/'+team_id
cje_user = envparams['user']
cje_pass = envparams['password']
local = testinfra.get_host("ssh://localhost")
results = local.command(
'curl -L -I --user '+cje_user+':'
+ cje_pass+' '+cje_master_url+'| grep HTTP\
|tail -1').stdout
if '200 OK' in results:
return True
else:
return False从更多的函数中删除了不必要的fixture,并且能够清理我的代码。谢谢!
https://stackoverflow.com/questions/53325313
复制相似问题