pytest用夹具参数化测试

时间:2020-03-11 16:20:40

标签: pytest fixtures

我想使用配置文件对测试进行参数化,这意味着我无法使用以下语法:

@pytest.fixture(
    params=[
        None,
        '172.16.xxx.xxx',
        '172.16.xxx.xx',
        '172.16.xxx.xxx',
        '172.16.xxx.xx'
    ],
    ids=[
        'Local',
        'Windows10',
        'Centos7.6',
        'Ubuntu18.04',
        'Mojave'
    ],
    scope='module'
)
def endpoint(request) -> str:
    yield request.param

所以我需要这样的东西:

@pytest.fixture(scope='module')
def endpoint(request) -> str:
    hosts = get_hosts_for_module(request.fspath, request.session.startdir)
    ...

    for host in hosts:
        host.setup_for_test()
        yield host.ip
        host.teardown_for_test()

每次运行len(hosts)次,每次运行一个不同的endpoint值。上面的语法似乎无效,并且仅针对hosts中的第一台主机运行测试。

您知道这里的正确语法是什么吗?

0 个答案:

没有答案
相关问题