[pytest-dev] Fixture ordering and scopes

Floris Bruynooghe flub at devork.be
Sat Mar 17 17:38:10 EDT 2018


Bruno Oliveira <nicoddemus at gmail.com> writes:

> On Fri, Mar 16, 2018 at 2:36 PM Floris Bruynooghe <flub at devork.be> wrote:
>
>> Bruno Oliveira <nicoddemus at gmail.com> writes:
>>
>> > So the order is important here, and leaving it undefined will require
>> > people to write this instead:
>> >
>> > @pytest.fixture(scope='session', autouse=True)
>> > def my_setup_logging(log_setup): pass
>> >
>> > @pytest.fixture(scope='session', autouse=True)
>> > def my_db_setup(my_setup_logging  ): pass
>> >
>> > While it works, it feels like unnecessary boilerplate.
>>
>> I'm not sure you finished your example there.  I'm actually curious what
>> you'd do.
>>
>
> I did finish it :)
>
> Because `my_db_setup` depends on `my_setup_logging`, this guarantees that
> they will execute in the correct order.

I'm still not following this, I'm probably being silly.  You have 4
autouse session-scoped fixtures but with a dependecy chain as
my_db_setup -> my_setup_logging -> setup_logging and then an unrelated
db_setup.  What am I missing here?

> It is a great example, it really stretches the composability of
>> fixtures.  If someone where to ask me this though I think the only thing
>> I'd come up with is this:
>>
>> import db
>>
>> @pytest.fixture(scope='session', autouse=True)
>> def my_db(log_setup):
>>     return db.db_setup()  # This needs to do the right thing.
>>
>>
> This is fine if `db_setup` is a fixture which doesn't depend on others, but
> if `db_setup` depends on other fixtures (say `tmpdir`) then this won't work
> so well.

True.  I'm not sure I have a good answer to this :-)


Cheers,
Floris


More information about the pytest-dev mailing list