Skip to content

Commit 1b9872a

Browse files
docs: update version changes about fixture
1 parent f160963 commit 1b9872a

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

Diff for: changelog/12989.improvement.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
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`

Diff for: doc/en/explanation/fixtures.rst

+59
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,65 @@ prefer. You can also start out from existing :ref:`unittest.TestCase
8888
style <unittest.TestCase>`.
8989

9090

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+
91150

92151
Fixture errors
93152
--------------

0 commit comments

Comments
 (0)