如何使用 py.test 框架访问测试脚本中的 config.option 命令行参数

5 python pytest python-2.7

setup我在使用 py.test 框架运行测试时从命令行参数获取值。

group.addoption("--setup", "--sC=", action="store", dest="setup", help="setup.")
def pytest_configure(config):
    print "config.option.setup: ", config.option.setup
Run Code Online (Sandbox Code Playgroud)

在这里,我可以使用 获取安装文件名config.option.setup,但与我在此处传递的文件名相同,我想从测试脚本中获取它。

如果我在测试脚本中添加相同的行,则会出现以下错误:

>       print "config.option.setup_config: ", config.option.setup_config
E       NameError: global name 'config' is not defined
Run Code Online (Sandbox Code Playgroud)

有人可以让我知道如何在测试脚本中访问 c​​onfig.option.setup 吗?

phd*_*phd 3

pytest_configure必须在文件中conftest.py。参见示例

option = None

def pytest_addoption(parser):
    parser.addoption("--setup", "--sC=", action="store", dest="setup", help="setup.")

def pytest_configure(config):
    global option
    option = config.option
    print "config.option.setup: ", config.option.setup
Run Code Online (Sandbox Code Playgroud)