您是否可以控制将在每个环境中调用py.test的命令行参数?如果是这种情况,您可以配置您的测试套件以添加命令行参数,而这些参数又可以被其他灯具用于相应地进行自我配置.
根据您的情况调整示例,它将类似于:
# content of conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption("--device",action="store",help="device: A or B")
@pytest.fixture
def param(request):
device = request.config.getoption("--device")
if device == 'A':
return X()
elif device == 'B':
return Y()
现在为您的测试文件:
# contents of test_Foo.py
def test_Foo(param):
foo = Foo(param)
# test foo
这允许你像这样调用py.test:
py.test --device=A
这将使test_Foo收到一个X对象.
您当然可以扩展它以接收yaml文件名,并从那里读取选项,相应地实现一个夹具.