[TIP] Modifying fixture parametrization from the command line

holger krekel holger at merlinux.eu
Wed Oct 29 22:29:58 PDT 2014


On Wed, Oct 29, 2014 at 12:33 -0700, Laurent Brack wrote:
> Hi Holger,
> 
> On 10/29/14, 10:31 AM, holger krekel wrote:
> >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
> 
> Thanks. I use this hook for other things and when I saw the email
> from Wouter this morning I was thinking of doing this, however ...
> 
> The advantage of using fixtures is that if say reaching
> smpt.gmail.com fails, the tests depending on that fixture would be
> marked as error which is what you want (in our case, we use devices)
> but I am not sure what I can do in pytest_generate_tests. Throwing
> an exception is not want you want to do off course, and ignoring the
> faulty device would lead to the test not showing up at all in the
> report (makes sense?).

Does "deferrring the setup" help here?
http://pytest.org/latest/example/parametrize.html#deferring-the-setup-of-parametrized-resources

If you use "indirect=True", fixture functions will still be called
and you can fail/skip/xfail them as needed.

> >
> >Pity there is no searchable pytest cookbook yet so you could have found
> >this more easily.
> Here is an idea for a book :)

Or a site :) I'd like to enter "parametrize command line" and end up
with the example i originally posted and or related examples.

best,
holger


> >
> >HTH,
> It does, it confirms this can't be done via fixtures ... at least
> not in the conventional way :)
> >holger
> >
> best/Laurent
> >
> >>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