Skip to content

Commit 027d233

Browse files
committed
Add minimal docs for package-scoped fixtures (experimental)
1 parent 3c19370 commit 027d233

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

changelog/2283.feature

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Pytest now supports package-level fixtures.
1+
New ``package`` fixture scope: fixtures are finalized when the last test of a *package* finishes. This feature is considered **experimental**, so use it sparingly.

doc/en/fixture.rst

+16
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,22 @@ instance, you can simply declare it:
258258
Finally, the ``class`` scope will invoke the fixture once per test *class*.
259259

260260

261+
``package`` scope (experimental)
262+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
263+
264+
.. versionadded:: 3.7
265+
266+
In pytest 3.7 the ``package`` scope has been introduced. Package-scoped fixtures
267+
are finalized when the last test of a *package* finishes.
268+
269+
.. warning::
270+
This functionality is considered **experimental** and may be removed in future
271+
versions if hidden corner-cases or serious problems with this functionality
272+
are discovered after it gets more usage in the wild.
273+
274+
Use this new feature sparingly and please make sure to report any issues you find.
275+
276+
261277
Higher-scoped fixtures are instantiated first
262278
---------------------------------------------
263279

src/_pytest/fixtures.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -960,16 +960,27 @@ def __call__(self, function):
960960
def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
961961
"""Decorator to mark a fixture factory function.
962962
963-
This decorator can be used (with or without parameters) to define a
964-
fixture function. The name of the fixture function can later be
965-
referenced to cause its invocation ahead of running tests: test
966-
modules or classes can use the pytest.mark.usefixtures(fixturename)
967-
marker. Test functions can directly use fixture names as input
963+
This decorator can be used, with or without parameters, to define a
964+
fixture function.
965+
966+
The name of the fixture function can later be referenced to cause its
967+
invocation ahead of running tests: test
968+
modules or classes can use the ``pytest.mark.usefixtures(fixturename)``
969+
marker.
970+
971+
Test functions can directly use fixture names as input
968972
arguments in which case the fixture instance returned from the fixture
969973
function will be injected.
970974
975+
Fixtures can provide their values to test functions using ``return`` or ``yield``
976+
statements. When using ``yield`` the code block after the ``yield`` statement is executed
977+
as teardown code regardless of the test outcome, and must yield exactly once.
978+
971979
:arg scope: the scope for which this fixture is shared, one of
972-
"function" (default), "class", "module" or "session".
980+
``"function"`` (default), ``"class"``, ``"module"``,
981+
``"package"`` or ``"session"``.
982+
983+
``"package"`` is considered **experimental** at this time.
973984
974985
:arg params: an optional list of parameters which will cause multiple
975986
invocations of the fixture function and all of the tests
@@ -990,10 +1001,6 @@ def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
9901001
to resolve this is to name the decorated function
9911002
``fixture_<fixturename>`` and then use
9921003
``@pytest.fixture(name='<fixturename>')``.
993-
994-
Fixtures can optionally provide their values to test functions using a ``yield`` statement,
995-
instead of ``return``. In this case, the code block after the ``yield`` statement is executed
996-
as teardown code regardless of the test outcome. A fixture function must yield exactly once.
9971004
"""
9981005
if callable(scope) and params is None and autouse is False:
9991006
# direct decoration

0 commit comments

Comments
 (0)