[TIP] Modifying fixture parametrization from the command line

holger krekel holger at merlinux.eu
Wed Oct 29 10:31:43 PDT 2014


Hi Laurent,

On Wed, Oct 29, 2014 at 09:22 -0700, Laurent Brack wrote:
> 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).

You can't do this through direct fixture parametrization.  But there
is a hook you can easily use, pytest_generate_tests.  Here is a full
example probably fitting quite exactly your use case:

http://pytest.org/latest/parametrize.html#basic-pytest-generate-tests-example

Pity there is no searchable pytest cookbook yet so you could have found
this more easily.

HTH,
holger


> 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
> 
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
> 



More information about the testing-in-python mailing list