[pytest-dev] Fixture ordering and scopes

Bruno Oliveira nicoddemus at gmail.com
Tue Mar 20 09:57:39 EDT 2018


On Tue, Mar 20, 2018 at 10:45 AM Floris Bruynooghe <flub at devork.be> wrote:

> On Tue, Mar 20 2018, Bruno Oliveira wrote:
> >
> > @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, db_setup): pass
>
> To me this looks like it still depends on the order of fixtures being
> used.  the end now depends on two chains: my_setup_logging -> log_setup
> and db_setup (single-item-chain).  But there's nothing, other then the
> rule under discussion, which prioritises one chain over the other.
>

Actually because of the fact that `my_db_setup` depends on
`my_setup_logging`, this guarantees that `my_setup_logging` will be
instantiated fist. Because the fixtures are autouse, any item will depend
on the fixtures in either of the following orders:

1) `my_setup_logging`, and then `my_db_setup`: if this is the case, then
that's the order we ultimately want;
2) `my_db_setup` and then `my_setup_logging`: here in order to instantiate
`my_db_setup`, we *need* to instantiate `my_setup_logging` first, thus
guaranteeing the order we want.

As we are discussing, the code above works with current pytest, but is more
bailerplate than the code that is possible with the PR we are discussing:

@pytest.fixture(scope='session', autouse=True)
def my_setup(log_setup, db_setup): pass

Mind you, I'm by now perfectly fine with the rule.  I'm only discussing
> it to understand the semantics of the world without that rule as just a
> curious exercise.
>

Sure thing! It is interesting as well, and somewhat surprising that this
discussion never came up before. :)

Cheers,
Bruno.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20180320/2fbc8d99/attachment.html>


More information about the pytest-dev mailing list