@@ -289,51 +289,30 @@ are finalized when the last test of a *package* finishes.
289
289
Use this new feature sparingly and please make sure to report any issues you find.
290
290
291
291
292
- Higher-scoped fixtures are instantiated first
293
- ---------------------------------------------
292
+ Order: Higher-scoped fixtures are instantiated first
293
+ ----------------------------------------------------
294
294
295
295
296
296
297
297
Within a function request for features, fixture of higher-scopes (such as ``session ``) are instantiated first than
298
298
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.
300
301
301
302
Consider the code below:
302
303
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
328
305
329
306
The fixtures requested by ``test_foo `` will be instantiated in the following order:
330
307
331
308
1. ``s1 ``: is the highest-scoped fixture (``session ``).
332
309
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
334
310
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.
337
316
338
317
339
318
.. _`finalization` :
0 commit comments