[pytest-dev] Working with other dependency injection systems

Kai Groner kai at gronr.com
Thu Sep 10 18:15:03 CEST 2015


On Thu, Sep 10, 2015 at 3:12 AM, holger krekel <holger at merlinux.eu> wrote:

> On Tue, Sep 08, 2015 at 14:33 -0400, Kai Groner wrote:
> > On Mon, Sep 7, 2015 at 4:01 AM, holger krekel <holger at merlinux.eu>
> wrote:
> >
> > > On Thu, Aug 13, 2015 at 17:47 -0400, Kai Groner wrote:
> > > > Here my problem is that py.test looks for a ``login`` fixture and
> when it
> > > > finds none it decides the test cannot proceed.
> > >
> > > You probably need to provide this "login" fixture even if it's empty
> > > and work is done in the hook above. not sure i get all your semantics
> > > 100 % though.
> > >
> >
> > The thing is that these aren't really fixtures and the dependencies are
> > selected by annotation rather than argument name.  What login means in
> one
> > test won't match what login means in another test.  Even when it is the
> > same service, it will be a new instance/partial created by a new injector
> > with new services.
> >
> > I do want to use py.test fixtures for parametrization.  Is there a way I
> > can tell py.test that certain arguments aren't fixtures and it shouldn't
> > worry about providing them?
>
> Not really.  Why can't you have a fixture function that selects an
> implementation based on annotations associated with the test function?


This would probably work, but we use injection pretty extensively, and this
would require a lot of extra fixture definitions.

Experimentally, I wrote the following (annotate is part of jeni):

def pytest_collection_modifyitems(session, config, items):
>     for item in items:

        if item.fixturenames and annotate.has_annotations(item.function):
>             arg_notes, kwarg_notes =
> annotate.get_annotations(item.function)
>             fixtureinfo = item._fixtureinfo
>             fixtureinfo.argnames = tuple(
>                     name for name in fixtureinfo.argnames
>                     if name not in kwarg_notes )
>             item.fixturenames[:] = (
>                     name for name in item.fixturenames
>                     if name not in kwarg_notes )
>

This does what I'm looking for, but having a sanctioned API would probably
be better than accessing item._fixtureinfo directly.


Kai
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20150910/f903349b/attachment.html>


More information about the pytest-dev mailing list