简体   繁体   English

基于命令行参数的参数化pytest函数

[英]Parametrize pytest function based on command line argument

I'm doing some selenium testing in Python, and I'm trying to set up my conftest.py to generate a new webdriver fixture that persists for the entire session, for each of the desired capabilities. 我正在用Python做一些硒测试,并且试图设置conftest.py来生成一个新的webdriver固定装置,该固定装置在整个会话期间都将持续存在,并且具有每种所需的功能。 The list of desired capabilities objects is specified via a YAML file, the path to which is specified at the command line. 所需功能对象的列表是通过YAML文件指定的,该文件的路径在命令行中指定。

Here's a super whittled down version of my conftest.py to illustrate what I have so far: 这是我的conftest.py的超级缩减版本,以说明到目前为止我所拥有的:

def pytest_addoption(parser):
    parser.addoption("-C", "--capabilities", action="store", default="capabilities.yaml")

def pytest_generate_tests(metafunc):
    if 'desired_capabilities' in metafunc.fixturenames:
        capabilities_file = metafunc.config.option.capabilities
        metafunc.parametrize('desired_capabilities', yaml.load(capabilities_file))

@pytest.fixture(scope="session")
def webdriver(desired_capabilities):
    # desired_capabilities should take on the different values of the list in the YAML file
    driver = util.create_webdriver(desired_capabilities)

But with this code, I am getting a whole bunch of ScopeMismatchError seg, ScopeMismatchError: You tried to access the 'function' scoped fixture 'desired_capabilities' with a 'session' scoped request object, involved factories (... omitted) 但是通过这段代码,我得到了一堆ScopeMismatchError seg, ScopeMismatchError: You tried to access the 'function' scoped fixture 'desired_capabilities' with a 'session' scoped request object, involved factories (... omitted)

How do I achieve this sort of thing? 我如何实现这种目标?

I think I've figured it out. 我想我已经知道了。 The issue was that I needed to add scope="session" to the keyword arguments of the call to parametrize . 问题是我需要在对parametrize的调用的关键字参数中添加scope="session"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM