gpt4 book ai didi

python - 基于命令行参数参数化 pytest 函数

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:26 26 4
gpt4 key购买 nike

我正在 Python 中进行一些 selenium 测试,并且我正在尝试设置我的 conftest.py 来生成一个新的 webdriver 固定装置,该固定装置在整个 session 中持续存在,对于每个所需的能力。所需功能对象的列表通过 YAML 文件指定,该文件的路径在命令行中指定。

这是我的 conftest.py 的 super 精简版本,以说明我到目前为止所拥有的内容:

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)

但是通过这段代码,我得到了一大堆ScopeMismatchError。例如,ScopeMismatchError:您尝试使用“ session ”范围的请求对象访问“函数”范围的固定装置“desired_capability”,涉及工厂(...省略)

我如何实现这样的目标?

最佳答案

我想我已经明白了。问题是我需要将 scope="session" 添加到调用 parametrize 的关键字参数中。

关于python - 基于命令行参数参数化 pytest 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30147953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com