[pytest-dev] Fixture ordering and scopes

Bruno Oliveira nicoddemus at gmail.com
Fri Mar 16 14:59:32 EDT 2018

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.

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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20180316/8adb70da/attachment.html>

More information about the pytest-dev mailing list