[pytest-dev] fixtures as context managers

holger krekel holger at merlinux.eu
Thu May 30 20:41:18 CEST 2013


On Tue, May 28, 2013 at 16:30 +0200, Andreas Pelme wrote:
> On Friday 24 May 2013 at 17:07, holger krekel wrote:
> > Then again, when i first saw the contextlib.contextmanager decorator 
> > i found it not very intuitive. Did anyone? It looks like a hack.
> > From that angle i'd rather go for requiring "contextyield=True" or
> > @pytest.contextfixture because that can be looked up in documentation
> > and thus is easier to read for people not familiar with yields/contextlib.
> 
> I cannot say that the contextmanager was obvious the first time I
> first saw it. But neither was generators and the yield statement. :-)
> I do think that using yield contextmanager-style that is a very good
> fit for fixtures. 

I recently gave a testing course where i didn't have the impression
"yield"-contextmanager would be very obvious to people who are not 
very familiar with Python (and i think there are a lot these days).
I know because i needed to explain "yield" to begin with, and
the context manager usage is a hack on top :)

OTOH some had problems with ``request.addfinalizer(fin)`` because
they hadn't passed functions around yet in their python code :)

I'd like to have preferably one main way explained in the docs and i am
not sure yet which, to be honest.

> People that are not familiar/happy with the idea contextmanager,
> generators and with statements might just want to continue use
> request.addfinalizer, which is totally fine.
> 
> About the API/naming: I like and prefer the current trunk behavior
> (just one API: pytest.fixture). If you find pytest.contextfixture more
> explicit and clear - go for it. I will be very happy with "context
> fixtures" whatever they are called. :-)

Right, we are going to have context fixtures one way or another in 2.4 :)

Ideally, we could ask some not-so-proficient-in-python people and present
them the addfinalizer() and the yield-context method and observe their 
reactions.  If any of you can do that with some colleagues, 
that'd be most appreciated.

cheers,
holger



> Cheers
> Andreas
> 
> 


More information about the Pytest-dev mailing list