pytest parameterize tests with a fixture

Viewed 59

I want to parameterize my tests with configuration files, which means I can't use the syntax:

@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

So I need something like this:

@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()

That runs every test len(hosts) times, with a different endpoint value each time. The syntax above doesn't seem to work and is only running the tests for the first host in hosts.

Any idea what's the right syntax here?

0 Answers
Related