[TIP] Modifying fixture parametrization from the command line

Laurent Brack lpbrac at dolby.com
Wed Oct 29 09:22:34 PDT 2014


Hi,

I have been struggling with this issue and couldn't find a way to make 
it work.

For sake of simplicity, say I take in the example from the documentation

http://pytest.org/latest/fixture.html#parametrizing-a-fixture

but want to give the user the ability, to change the list of mail 
servers from the command line (say smtp.gmail.com).

Basically

@pytest.fixture(scope="module",
                 params=["merlinux.eu", "mail.python.org"])
def smtp(request):
     smtp = smtplib.SMTP(request.param)
     ...

becomes

@pytest.fixture(scope="module",
                 params=["smtp.gmail.com"])
def smtp(request):
    ...

At runtime.

The problem I had is that list given in "params" is created when 
conftest is loaded which is before the pytest_addoption and 
pytest_configure are invoked.

I tried to use a global variable (yuk) and modify the list content in 
the hook hoping that the fixture keeps the same reference but this 
didn't work.

Is there anyway I can do this?

Thanks in advance.

/Laurent



More information about the testing-in-python mailing list