我有一个conftest.py 文件,如下所示:
from testdevice import TestDevice
from pytest import fixture
def pytest_addoption(parser):
parser.addoption("--n", action="store", default="Name")
@fixture()
def name(request):
return request.config.getoption("--n")
def pytest_configure(config):
"""
Allows plugins and conftest files to perform initial configuration.
This hook is called for every plugin and initial conftest
file after command line options have been parsed.
"""
def pytest_sessionstart(session):
"""
Called after the Session object has been created and
before performing collection and entering the run test loop.
"""
name = 'device_name'
TestDevice(name)
def pytest_sessionfinish(session, exitstatus):
"""
Called after whole test run finished, right before
returning the exit status to the system.
"""
def pytest_unconfigure(config):
"""
called before test process is exited.
"""
我正在测试嵌入式设备,在测试中使用 ssh。在测试之前,我想使用 TestDevice(name) 类准备设备。 “name”是包含设备信息的字典的键。
而不是在会话启动中硬编码名称。我创建了一个可以访问名称参数的固定装置,但是我只能在测试中使用该固定装置。我无法访问“pytest_sessionstart”中的固定装置,因为我无法访问“请求”参数。
可以采取任何措施来访问“pytest_sessionstart”上的 python 参数吗?
您无需使用额外的固定装置。
您可以通过会话对象在 pytest_sessionstart 中获取名称:
def pytest_addoption(parser):
parser.addoption("--n", action="store", default="Name")
def pytest_sessionstart(session):
name = session.config.getoption('--n'):
TestDevice(name)