Skip to content

Commit d23fbab

Browse files
Add autouse fixture order information (#3404).
A case with a fixture use both as an autouse and explititly was raised. This case sounds too narrow to add to documentation (and could be misleading for people learning pytest with explicitely using an autouse fixture). At the same time there was no documentation on the autouse vs regular fixture order, therefore this commit adds such an information. Code sample grew and it was extracted to the file.
1 parent 8052c7c commit d23fbab

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
# fixtures documentation order example
4+
order = []
5+
6+
7+
@pytest.fixture(scope="session")
8+
def s1():
9+
order.append("s1")
10+
11+
12+
@pytest.fixture(scope="module")
13+
def m1():
14+
order.append("m1")
15+
16+
17+
@pytest.fixture
18+
def f1(f3):
19+
order.append("f1")
20+
21+
22+
@pytest.fixture
23+
def f3():
24+
order.append("f3")
25+
26+
27+
@pytest.fixture(autouse=True)
28+
def a1():
29+
order.append("a1")
30+
31+
32+
@pytest.fixture
33+
def f2():
34+
order.append("f2")
35+
36+
37+
def test_order(f1, m1, f2, s1):
38+
assert order == ["s1", "m1", "a1", "f3", "f1", "f2"]

doc/en/fixture.rst

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -289,51 +289,30 @@ are finalized when the last test of a *package* finishes.
289289
Use this new feature sparingly and please make sure to report any issues you find.
290290

291291

292-
Higher-scoped fixtures are instantiated first
293-
---------------------------------------------
292+
Order: Higher-scoped fixtures are instantiated first
293+
----------------------------------------------------
294294

295295

296296

297297
Within a function request for features, fixture of higher-scopes (such as ``session``) are instantiated first than
298298
lower-scoped fixtures (such as ``function`` or ``class``). The relative order of fixtures of same scope follows
299-
the declared order in the test function and honours dependencies between fixtures.
299+
the declared order in the test function and honours dependencies between fixtures. Autouse fixtures will be
300+
instantiated before explicitly used fixtures.
300301

301302
Consider the code below:
302303

303-
.. code-block:: python
304-
305-
@pytest.fixture(scope="session")
306-
def s1():
307-
pass
308-
309-
310-
@pytest.fixture(scope="module")
311-
def m1():
312-
pass
313-
314-
315-
@pytest.fixture
316-
def f1(tmpdir):
317-
pass
318-
319-
320-
@pytest.fixture
321-
def f2():
322-
pass
323-
324-
325-
def test_foo(f1, m1, f2, s1):
326-
...
327-
304+
.. literalinclude:: example/fixtures/test_fixtures_order.py
328305

329306
The fixtures requested by ``test_foo`` will be instantiated in the following order:
330307

331308
1. ``s1``: is the highest-scoped fixture (``session``).
332309
2. ``m1``: is the second highest-scoped fixture (``module``).
333-
3. ``tmpdir``: is a ``function``-scoped fixture, required by ``f1``: it needs to be instantiated at this point
334310
because it is a dependency of ``f1``.
335-
4. ``f1``: is the first ``function``-scoped fixture in ``test_foo`` parameter list.
336-
5. ``f2``: is the last ``function``-scoped fixture in ``test_foo`` parameter list.
311+
3. ``a1``: is a ``function``-scoped ``autouse`` fixture: it will be instantiated before other fixtures
312+
within the same scope.
313+
4. ``f3``: is a ``function``-scoped fixture, required by ``f1``: it needs to be instantiated at this point
314+
5. ``f1``: is the first ``function``-scoped fixture in ``test_foo`` parameter list.
315+
6. ``f2``: is the last ``function``-scoped fixture in ``test_foo`` parameter list.
337316

338317

339318
.. _`finalization`:

0 commit comments

Comments
 (0)