Skip to content

Commit 1e9ff9a

Browse files
author
Chris NeJame
committed
Clarify fixture order and provide images for visual learners
The documentation previously stated that fixture execution order was dictated by the order fixtures were requested in, but this isn't a very reliable method controlling fixture order and can lead to some confusing situations. The only reliable way to control fixture order is based on scope, fixture dependencies, and autouse. This reflects that in the documentation, and provides several examples and images to help clarify that.
1 parent 4cc4ebf commit 1e9ff9a

19 files changed

+1318
-63
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Charles Cloud
5555
Charles Machalow
5656
Charnjit SiNGH (CCSJ)
5757
Chris Lamb
58+
Chris NeJame
5859
Christian Boelsen
5960
Christian Fetzer
6061
Christian Neumüller
Lines changed: 101 additions & 0 deletions
Loading
Lines changed: 104 additions & 0 deletions
Loading

doc/en/example/fixtures/test_fixtures_order.py

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def order():
6+
return []
7+
8+
9+
@pytest.fixture
10+
def a(order):
11+
order.append("a")
12+
13+
14+
@pytest.fixture
15+
def b(a, order):
16+
order.append("b")
17+
18+
19+
@pytest.fixture(autouse=True)
20+
def c(b, order):
21+
order.append("c")
22+
23+
24+
@pytest.fixture
25+
def d(b, order):
26+
order.append("d")
27+
28+
29+
@pytest.fixture
30+
def e(d, order):
31+
order.append("e")
32+
33+
34+
@pytest.fixture
35+
def f(e, order):
36+
order.append("f")
37+
38+
39+
@pytest.fixture
40+
def g(f, c, order):
41+
order.append("g")
42+
43+
44+
def test_order_and_g(g, order):
45+
assert order == ["a", "b", "c", "d", "e", "f", "g"]
Lines changed: 64 additions & 0 deletions
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(scope="class")
5+
def order():
6+
return []
7+
8+
9+
@pytest.fixture(scope="class", autouse=True)
10+
def c1(order):
11+
order.append("c1")
12+
13+
14+
@pytest.fixture(scope="class")
15+
def c2(order):
16+
order.append("c2")
17+
18+
19+
@pytest.fixture(scope="class")
20+
def c3(order, c1):
21+
order.append("c3")
22+
23+
24+
class TestClassWithC1Request:
25+
def test_order(self, order, c1, c3):
26+
assert order == ["c1", "c3"]
27+
28+
29+
class TestClassWithoutC1Request:
30+
def test_order(self, order, c2):
31+
assert order == ["c1", "c2"]

0 commit comments

Comments
 (0)