File tree 2 files changed +60
-1
lines changed
2 files changed +60
-1
lines changed Original file line number Diff line number Diff line change 1
- A warning is now issued when tests incorrectly use :class: `@pytest.fixture `, whereas this was previously silently ignored.
1
+ Issues a warning when a function is not collected as a test case just because it uses :py:func: `@pytest.fixture `
Original file line number Diff line number Diff line change @@ -88,6 +88,65 @@ prefer. You can also start out from existing :ref:`unittest.TestCase
88
88
style <unittest.TestCase>`.
89
89
90
90
91
+ Distinguishing fixtures and tests
92
+ ------------------------------------
93
+
94
+ Fixtures and tests may seem similar: both are functions (or methods) and both can utilize fixtures.
95
+ However, there is a fundamental difference:
96
+
97
+ - **Tests ** are the leading role.
98
+ They can actively use :ref: `mark <mark >` and fixtures.
99
+
100
+ - **Fixtures ** are supporting role.
101
+ They cannot use mark or tests and only be used directly or indirectly by test cases.
102
+
103
+
104
+ Two classic traps:
105
+
106
+
107
+ - Will not fail, because adding any tags to the fixture is invalid
108
+
109
+ .. code-block :: python
110
+
111
+ import warnings
112
+
113
+
114
+ @pytest.fixture
115
+ @pytest.mark.filterwarnings (" error" ) # fixture cannot use mark
116
+ def server ():
117
+ print (" fixture is running!" )
118
+
119
+
120
+ def test_foo (server ):
121
+ warnings.warn(UserWarning (" api v1, should use functions from v2" ))
122
+
123
+
124
+
125
+ - No code will execute, because the ``test_foo `` becomes a fixture after using ``@pytest.fixture ``
126
+
127
+
128
+ .. code-block :: python
129
+
130
+ @pytest.fixture
131
+ def server ():
132
+ print (" fixture is running!" )
133
+
134
+
135
+ @pytest.fixture # turn test case into fixtures
136
+ def test_foo (server ):
137
+ warnings.warn(UserWarning (" api v1, should use functions from v2" ))
138
+
139
+
140
+
141
+ .. versionadded :: 8.0
142
+
143
+ Applying a mark to a fixture function now issues a warning and will become an error in pytest 9.0.
144
+
145
+
146
+ .. versionadded :: 8.4
147
+
148
+ Issues a warning when a function is not collected as a test case just because it uses ``@pytest.fixture ``
149
+
91
150
92
151
Fixture errors
93
152
--------------
You can’t perform that action at this time.
0 commit comments