[py-dev] optionally considering setup (needsdb usecase)

holger krekel holger at merlinux.eu
Sat Aug 4 14:13:56 CEST 2012


Hi Floris, all,

i think the below from an earlier thread is an outstanding issue for the
new API ...

On Sat, Jun 30, 2012 at 12:26 +0100, Floris Bruynooghe wrote:
> As an aside however, one of my usecases for merged request/item
> objects was so I could put setup in a session-wide scoped funcarg but
> also automatically request this funcarg based on a mark::
> 
>    def pytest_runtest_setup(item):
>        if 'needsdb' in item.keywords:  # or a more explicit API
>           item.getresource('db')
> 
> I understand that this will still be possible via::
> 
>    def pytest_runtest_setup(item):
>        if 'needsdb' in item.keywords:
>            item.session.getresource('db')
> 
> Or something similar to that.

With the current @setup API this is not possible but it should be.  I'd like
to understand the exact use case a bit.  What do you do with the "db"
object here?  I guess you cause side effects because you would otherwise
just request a funcarg in the tests, right?

If so, then i can imagine the following solution:

    @pytest.setup(enabled=myhelper)
    def perform_side_effect_with(db):
        ...

The "enabled" helper would be called during collection so that
pytest gets to know which tests will actually execute the setup
function and its (potentially parametrized) required resources.  
It could look like this::

    def myhelper(collectioncontext):
        return "needsdb" in collectioncontext.markers:

and collectioncontext also carries module/class/function (depending on
the scope specified with the setup).  If the helper returns True then
the setup is considered and thus receives the DB object.  Do you
think this would solve your use case? (Collectioncontext would not
have a addfinalizer() and might in the future offer more collection-specific
things).

best,
holger

          
          

> 
> 
> Regards,
> Floris
> 
> -- 
> Debian GNU/Linux -- The Power of Freedom
> www.debian.org | www.gnu.org | www.kernel.org
> _______________________________________________
> py-dev mailing list
> py-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/py-dev
> 



More information about the Pytest-dev mailing list