[py-dev] parametrized resources (Re: Generic funcarg hook)

Floris Bruynooghe flub at devork.be
Wed Aug 11 20:28:43 CEST 2010


On Tue, Aug 10, 2010 at 12:10:13PM +0200, holger krekel wrote:
> On Tue, Aug 10, 2010 at 11:01 +0200, holger krekel wrote:
> > FYI, i am considering introducing a slightly higher level 
> > mechanism on top of funcargs, dealing with (parametrizable)
> > resources.  Something like:
> > 
> > @py.test.mark.resourcefactory(scope="session", argnames=['foo', 'bar'])
> > def myfactory(request):
> >     if request.argname == "foo":
> >         ...
> >     elif request.argname == "bar":
> >         ... 
[...]
> Moreover, regarding the "parametrized resources" aspect, 
> there would be a new decorator:  
> 
>     @py.test.mark.parametrize(foo=iterable)
>     def test_function(foo):
>         ... invoked for multiple foo instances

Not sure how much I like these examples.  But the use of decorators
could make things clearer maybe, I think something like this might
make things easier to write:

   @py.test.cached(scope='session')  # or py.test.mark.cached
   def pytest_funcarg__foo(request):
       pass

Here the actual function would only be called once for the session.

   @py.test.parametrize(iterable)
   def pytest_funcarg__foo(request, args, from, iterable):
       pass

In this case the function would be called as many times as the
iterable has items.  Each item in the iterable would be the arguments
to pass to the function.  The iterable might have to work both with
['arg1', 'arg2', ...] as well as [('spam1', 'eggs1'), ('spam2',
'eggs2'), ...] to be easy to use.

And of course nesting these decorators would be useful too.  Here's
how I'd use this in real code I have:

@py.test.cached(scope='session')
@py.test.parametrize(['v1', 'v2c', 'v3'])
def pytest_funcarg__snmpcfg(request, version):
    if version in ('v1', 'v2c'):
        cfg = check_snmp_v12(version)
    elif version == 'v3':
        cfg = check_snmp_v3()
    if not cfg:
        py.test.skip('No SNMP%s agent available' % version)
    return SnmpWrapper(cfg)

I expect this function to be called 3 times during the whole session
and any test function asking for "snmpcfg" will get called 3 times
(some of those 3 might skip instead in this case).  This should
probably work independed of the order of the decorators (the order
shown reads a little funny I guess).


Anyway, just my toughts after reading your description

Floris

-- 
Debian GNU/Linux -- The Power of Freedom
www.debian.org | www.gnu.org | www.kernel.org



More information about the Pytest-dev mailing list