[py-dev] Resource providers

holger krekel holger at merlinux.eu
Tue Jun 26 16:15:11 CEST 2012


On Tue, Jun 26, 2012 at 15:07 +0100, Floris Bruynooghe wrote:
> On 25 June 2012 16:23, holger krekel <holger at merlinux.eu> wrote:
> > On Mon, Jun 25, 2012 at 15:21 +0100, Floris Bruynooghe wrote:
> >> On 25 June 2012 14:29, holger krekel <holger at merlinux.eu> wrote:
> >> > On Mon, Jun 25, 2012 at 10:55 +0100, Floris Bruynooghe wrote:
> >> >> The concrete example I have now is that it could be nice in
> >> >> pytest-django to be able to request e.g. "Users" which is a model
> >> >> class used to access the User table in the database.  Currently this
> >> >> is only possible by someone explicitly defining pytest_funcarg__Users,
> >> >> but Django allows you to dynamically look up all the models in the
> >> >> database so there is no reason this can't be build automatically.
> >> >>
> >> >> I think this is what the API you proposed was for, but as I said I
> >> >> can't remember the details.  And in this case I might be less
> >> >> enthusiastic in postponing it's implementation to a later release ;-)
> >> >
> >> > It's probably true that we could invent an register-factory API for this.
> >> >
> >> > However, what about a single "models" object (done traditionally
> >> > with a pytest_funcarg__models definition) which itself provides
> >> > an API to give "Users" or others data?
> >>
> >> Yes of course, that is what I currently have in my conftest.py.  But
> >> it would still be a nice thing to be able to do and a nice example of
> >> functionality I have wished I had before.  Hence I was wondering if
> >> the API you talked about yesterday would support it.
> >
> > I guess it could, for example, look like this::
> >
> >    def pytest_configure(config):  # [1]
> >        def createmodel(name, node):
> >            """ return django model object. """
> >            # node can be None, Directory, Module, Class, Item, etc.
> >            # (code to compute model)
> >            return model
> >
> >        for name in modelnames:
> >            config.register_factory(name, createmodel)
> 
> I think for addressing this specific usecase I was more imagining a
> standard pytest hook:
> 
>    def pytest_resource_factory(name, item):
>        """The docstring which can show up in --funcargs"""
>        if name == 'User':
>            return models.User

The docstring cannot show up in --funcargs because it would
be unclear to which name it actually belongs without executing it.
And just showing that there is a generic factory hook would not
be very informative.

> That way it can be scoped per-directory in the conftest.py files and
> used in plugins, which would be scoped globally.
> 
> I appreciate that this does not provide the other benefits nor can it
> be used to implement funcargs themself.  So maybe there is a need for
> an API you just describe which would allow one to implement what I
> just described in a plugin as well as write the funcargs as a plugin.
> But I'm much less comfortable suggesting what that API should be like,
> I do not fully know the innards of py.test like you do.

thanks for the feedback.  I am going to see if i get a day
to play around with an implementation for the suggested api.

best,
holger



More information about the Pytest-dev mailing list