简体   繁体   English

如何从函数中的pytest固定装置获取返回值,所以我不需要使用附加参数调用此函数?

[英]how to get return value from pytest fixture in a function so I don't need to call this function with additional parameter?

I have the following fixture in conftest.py that returns an environment settings dictionary like user, password etc: 我在conftest.py中具有以下固定装置,该固定装置返回环境设置字典,例如用户,密码等:

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

Then I have module like: 然后我有像这样的模块:

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

and a test that uses this module like: 以及使用此模块的测试,例如:

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)

Can I get rid of the envparams parameter from module function somehow so I can call it without additional parameter? 我可以以某种方式摆脱模块函数中的envparams参数,以便在没有附加参数的情况下调用它吗? like: 喜欢:

security.request_master_url(envparams)

I only need to setup this fixture once in a session. 我只需要在一次会话中设置一次这个灯具。 I tried to use: 我尝试使用:

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

But, I am not sure how to get values returned from this fixture. 但是,我不确定如何获取此固定装置返回的值。

Well, I've done as hoefling suggested. 好吧,我已经按照建议的方式做了。

Created small function in conftest.py: 在conftest.py中创建了一个小函数:

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

and call it from my module functions where needed. 并在需要时从我的模块函数中调用它。 Example function looks like below: 示例函数如下所示:

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

removed unnecessary fixture from many more functions and was able to clean my code a lot. 从许多其他功能中删除了不必要的固定装置,并能够大量清理我的代码。 Thanks! 谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我只能通过在测试脚本中使用夹具(来自 pytest)来获取函数作为返回值? - Why do I only get a function as a return value by using a fixture (from pytest) in a test script? 如何将测试用例参数传递到从一个 function 使用的 pytest 夹具中? - How to pass testcase parameter into a pytest fixture that is used from one function? 从 Pytest 夹具中调用自定义函数 - Call custom function from within Pytest fixture py.test:如何从@ pytest.fixture中访问test_function的参数 - py.test : How do I access a test_function's parameter from within an @pytest.fixture 如何在每个参数上执行 pytest 夹具以测试 function? - How to execute pytest fixture on each parameter for test function? 当我每次在命令提示符中调用 function 时我不需要再次打开文件,我该如何做到这一点? - How do I make it so when everytime I call a function in the Command Prompt I don't need to open the file again? 如何使用将在 pytest 测试用例中用作参数化的夹具函数和数据的返回 - How can I use return of fixture function and data which will be used as parameterized in pytest test case 使用pytest,我可以在测试功能之外获得夹具值吗? - Using pytest, can I get fixture values outside test function? 如何使用 scope = session 从 pytest 夹具返回多个值 - How to return multiple value from a pytest fixture with scope = session 如何将灯具返回的值作为参数传递给函数 - how to pass the value returned by the fixture as a parameter to the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM