Skip to content

Commit 29289b4

Browse files
committed
Add documentation for "invocation" scoped fixture
1 parent 7a2058e commit 29289b4

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

doc/en/fixture.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ first execute with one instance and then finalizers are called
603603
before the next fixture instance is created. Among other things,
604604
this eases testing of applications which create and use global state.
605605

606-
The following example uses two parametrized funcargs, one of which is
606+
The following example uses two parametrized fixture, one of which is
607607
scoped on a per-module basis, and all the functions perform ``print`` calls
608608
to show the setup/teardown flow::
609609

@@ -863,6 +863,14 @@ All test methods in this TestClass will use the transaction fixture while
863863
other test classes or functions in the module will not use it unless
864864
they also add a ``transact`` reference.
865865

866+
invocation-scoped fixtures
867+
--------------------------
868+
869+
pytest 3.0 introduced a new advanced scope for fixtures: ``"invocation"``. Fixtures marked with
870+
this scope can be requested from any other scope, providing a version of the fixture for that scope.
871+
872+
See more in :ref:`invocation_scoped_fixture`.
873+
866874
Shifting (visibility of) fixture functions
867875
----------------------------------------------------
868876

doc/en/invocation-fixture.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
.. _invocation_scoped_fixture:
2+
3+
Invocation-scoped fixtures
4+
==========================
5+
6+
.. versionadded:: 3.0
7+
8+
.. note::
9+
This feature is experimental, so if decided that it brings too much problems
10+
or considered too complicated it might be removed in pytest ``3.1``.
11+
12+
``tmpdir`` and ``monkeypatch`` might become ``invocation`` scoped
13+
fixtures in the future if decided to keep invocation-scoped fixtures.
14+
15+
Fixtures can be defined with ``invocation`` scope, meaning that the fixture
16+
can be requested by fixtures from any scope, but when they do they assume
17+
the same scope as the fixture requesting it.
18+
19+
An ``invocation``-scoped fixture can be requested from different scopes
20+
in the same test session, in which case each scope will have its own copy.
21+
22+
Example
23+
-------
24+
25+
Consider a fixture which manages external process execution:
26+
this fixture provides auxiliary methods for tests and fixtures to start external
27+
processes while making sure the
28+
processes terminate at the appropriate time. Because it makes sense
29+
to start a webserver for the entire session and also to execute a numerical
30+
simulation for a single test function, the ``process_manager``
31+
fixture can be declared as ``invocation``, so each scope gets its own
32+
value and can manage processes which will live for the duration of the scope.
33+
34+
.. code-block:: python
35+
36+
import pytest
37+
38+
@pytest.fixture(scope='invocation')
39+
def process_manager():
40+
m = ProcessManager()
41+
yield m
42+
m.shutdown_all()
43+
44+
45+
@pytest.fixture(scope='session')
46+
def server(process_manager):
47+
process_manager.start(sys.executable, 'server.py')
48+
49+
50+
@pytest.fixture(scope='function')
51+
def start_simulation(process_manager):
52+
import functools
53+
return functools.partial(process_manager.start,
54+
sys.executable, 'simulator.py')
55+
56+
57+
In the above code, simulators started using ``start_simulation`` will be
58+
terminated when the test function exits, while the server will be kept
59+
active for the entire simulation run, being terminated when the test session
60+
finishes.
61+

0 commit comments

Comments
 (0)