Skip to content
Advertisement

How to impose order on fixtures in pytest?

I am trying to use pytest-dependency to make fixtures happen in order, regardless of how they are named, and regardless of the order in which they appear in a test’s argument list.

The reason I need this, is to create fixtures that require initializations, that depend on other fixtures that require initializations, and they must happen in order. I have quite a few of these, and I don’t want to rely on naming or on order in the argument list.

I also don’t want to use pytest_sessionstart, because it can’t take fixture inputs, which causes very unclean code.


The trivial example in the doc shows how to create programmed dependencies for tests:

JavaScript

This works with output:

JavaScript

Now I want to do the same for fixtures.

My attempt:

conftest.py:

JavaScript

and test_sanity.py:

JavaScript

Outputs

JavaScript

I expected the error on zzzshould_happen_first.


Is there a way to impose ordering on fixtures, such that

  1. Their name is ignored
  2. Their order in the argument list is ignored
  3. Other pytest features such as autouse can still be applied

Advertisement

Answer

You can give a fixture as a dependency directly with pytest. Something like this:

JavaScript

It gives what you want:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement