Victor Olex
Victor Olex

Reputation: 1498

Pytest - How to override fixture parameter list from command line?

Consider the following fixture

@pytest.fixture(params=['current', 'legacy'])
def baseline(request):
    return request.param

I wonder if there is a way to launch pytest so it overrides the fixture parameter list with the value(s) given on the command line, i.e.:

pytest --baseline legacy tests/

The above should effectively result in params=['legacy'].

Upvotes: 3

Views: 2164

Answers (1)

hoefling
hoefling

Reputation: 66421

Go with dynamic parametrization via Metafunc.parametrize:

# conftest.py
import pytest


@pytest.fixture
def baseline(request):
    return request.param


def pytest_addoption(parser):
    parser.addoption('--baseline', action='append', default=[],
        help='baseline (one or more possible)')


def pytest_generate_tests(metafunc):
    default_opts = ['current', 'legacy']
    baseline_opts = metafunc.config.getoption('baseline') or default_opts
    if 'baseline' in metafunc.fixturenames:
        metafunc.parametrize('baseline', baseline_opts, indirect=True)

Usage without parameters yields two default tests:

$ pytest test_spam.py -sv
...
test_spam.py::test_eggs[current] PASSED
test_spam.py::test_eggs[legacy] PASSED

Passing --baseline overwrites the defaults:

$ pytest test_spam.py -sv --baseline=foo --baseline=bar --baseline=baz
...
test_spam.py::test_eggs[foo] PASSED
test_spam.py::test_eggs[bar] PASSED
test_spam.py::test_eggs[baz] PASSED

You can also implement "always-in-use" defaults, so additional params are always added to them:

def pytest_addoption(parser):
    parser.addoption('--baseline', action='append', default=['current', 'legacy'],
        help='baseline (one or more possible)')


def pytest_generate_tests(metafunc):
    baseline_opts = metafunc.config.getoption('baseline')
    if 'baseline' in metafunc.fixturenames and baseline_opts:
        metafunc.parametrize('baseline', baseline_opts, indirect=True)

Now the test invocation will always include current and legacy params:

$ pytest test_spam.py -sv --baseline=foo --baseline=bar --baseline=baz
...
test_spam.py::test_eggs[current] PASSED
test_spam.py::test_eggs[legacy] PASSED
test_spam.py::test_eggs[foo] PASSED
test_spam.py::test_eggs[bar] PASSED
test_spam.py::test_eggs[baz] PASSED

Upvotes: 3

Related Questions