[pytest-dev] fixtures as context managers

Andreas Pelme andreas at pelme.se
Fri May 24 16:36:27 CEST 2013


On Thursday 9 May 2013 at 15:56, holger krekel wrote:
> This is probably used by very few people but to be on the safe side,
> we probably should introduce a flag like this:
> 
> @pytest.fixture(ctx=True) # signal this is a context manager style fixture
> def fix():
> yield 1
> 
> What do you think? Any other suggestions for the flag name?
> 
> I'd rather not introduce something like @pytest.contextfixture
> because it would be a duplication of the API (scope, params).
> But i am open to be convinced otherwise.

I agree that another API like contextfixture should be avoided.

We had a short discussion on IRC about this, Holger had the idea of doing the opposite if ctx=True - a new default argument "yielding=False" which would make it possible to restore the old behavior by putting yielding=True on fixtures that would be affected by this.

A fixture that is a generator that currently looks like this

@pytest.fixture
def fix():
    yield 1
    yield 2

Would then have to be changed to

@pytest.fixture(yielding=True)
def fix():
    yield 1
    yield 2

This is backward incompatible, but given that it seems questionable if "generator fixtures" useful/is used, with a note in the release notes and documentation I think this could be a good compromise.

I will be happy to give this a try if it is decided this could be a good approach!

Cheers
Andreas




More information about the Pytest-dev mailing list