[pytest-dev] fixtures as context managers

Bruno Oliveira nicoddemus at gmail.com
Sun May 5 15:40:29 CEST 2013


Very nice! I always thought the "request.addfinalizer" mechanism somewhat
cumbersome. :)


On Sun, May 5, 2013 at 10:05 AM, holger krekel <holger at merlinux.eu> wrote:

> Hi all,
>
> after having been asked by Andreas Pelme and Vladimir Keleshev, repeatedly,
> i finally tried to allow fixture functions to be context managers like
> this:
>
>     @pytest.fixture
>     def myfixture():
>         print "setup"
>         yield val
>         print "teardown"
>
> With this, you don't need to use "request.addfinalizer" - pytest does
> this by detecting your yield.  I checked this into trunk to allow
> experimentation - it seems to work fine with scopes and parametrization.
> For pytest, it's really just a slightly different "fixture function
> calling"
> protocol, contained in this commit:
>
>
> https://bitbucket.org/hpk42/pytest/commits/b93ac0cdae02effaa3c136a681cc45bba757fe46
>
> Note that i don't plan to extend this yield mechanism to parametrization
> as that would meanparametrization at test execution time, rather than at
> collection time.  It would be both conceptually and implementation wise
> hard to do i think.
>
> The change is committed to trunk, and you can also do:
>
>     pip install -i http://pypi.testrun.org -U pytest
>
> to get pytest-2.3.6.dev2 at least.
>
> feedback welcome, actually without people actively
> wanting it, it might be thrown out again ;)
>
> holger
>
>
> _______________________________________________
> Pytest-dev mailing list
> Pytest-dev at python.org
> http://mail.python.org/mailman/listinfo/pytest-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20130505/32993ec1/attachment.html>


More information about the Pytest-dev mailing list