You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts.rst
+34-14Lines changed: 34 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,30 +47,50 @@ Assigning neighboring tests to different event loop scopes is discouraged as it
47
47
Test discovery modes
48
48
====================
49
49
50
-
Pytest-asyncio provides two modes for test discovery, *strict* and *auto*. This can be set through Pytest's ``--asyncio-mode`` command line flag, or through the configuration file:
50
+
Pytest-asyncio provides two modes for test discovery, *strict* and *auto*.
51
+
This can be set through Pytest's ``--asyncio-mode`` command line flag,
52
+
or through the configuration file.
51
53
52
-
.. code-block:: toml
54
+
.. tabs::
53
55
54
-
[tool.pytest.ini_options]
55
-
asyncio_mode = "auto" # or "strict"
56
+
.. group-tab:: Strict mode
56
57
57
-
Strict mode
58
-
-----------
58
+
.. code-block:: toml
59
59
60
-
In strict mode pytest-asyncio will only run tests that have the *asyncio* marker and will only evaluate async fixtures decorated with ``@pytest_asyncio.fixture``. Test functions and fixtures without these markers and decorators will not be handled by pytest-asyncio.
60
+
[tool.pytest.ini_options]
61
+
asyncio_mode = "strict"
61
62
62
-
This mode is intended for projects that want so support multiple asynchronous programming libraries as it allows pytest-asyncio to coexist with other async testing plugins in the same codebase.
63
+
In strict mode pytest-asyncio will only run tests that have the *asyncio* marker
64
+
and will only evaluate async fixtures decorated with ``@pytest_asyncio.fixture``.
65
+
Test functions and fixtures without these markers and decorators will not be
66
+
handled by pytest-asyncio.
63
67
64
-
Pytest automatically enables installed plugins. As a result pytest plugins need to coexist peacefully in their default configuration. This is why strict mode is the default mode.
68
+
This mode is intended for projects that want to support multiple asynchronous
69
+
programming libraries as it allows pytest-asyncio to coexist with other async
70
+
testing plugins in the same codebase.
65
71
66
-
Auto mode
67
-
---------
72
+
Pytest automatically enables installed plugins. As a result pytest plugins
73
+
need to coexist peacefully in their default configuration. This is why strict
74
+
mode is the default mode.
68
75
69
-
In *auto* mode pytest-asyncio automatically adds the *asyncio* marker to all asynchronous test functions. It will also take ownership of all async fixtures, regardless of whether they are decorated with ``@pytest.fixture`` or ``@pytest_asyncio.fixture``.
76
+
.. group-tab:: Auto mode
70
77
71
-
This mode is intended for projects that use *asyncio* as their only asynchronous programming library. Auto mode makes for the simplest test and fixture configuration and is the recommended default.
78
+
.. code-block:: toml
72
79
73
-
If you intend to support multiple asynchronous programming libraries, e.g. *asyncio* and *trio*, strict mode will be the preferred option.
80
+
[tool.pytest.ini_options]
81
+
asyncio_mode = "auto"
82
+
83
+
In *auto* mode pytest-asyncio automatically adds the *asyncio* marker to all
84
+
asynchronous test functions. It will also take ownership of all async fixtures,
85
+
regardless of whether they are decorated with ``@pytest.fixture`` or
86
+
``@pytest_asyncio.fixture``.
87
+
88
+
This mode is intended for projects that use *asyncio* as their only asynchronous
89
+
programming library. Auto mode makes for the simplest test and fixture
90
+
configuration and is the recommended default.
91
+
92
+
If you intend to support multiple asynchronous programming libraries,
93
+
e.g. *asyncio* and *trio*, strict mode will be the preferred option.
The :ref:`configuration/asyncio_default_fixture_loop_scope` configuration option sets the default event loop scope for asynchronous fixtures. The following code snippets configure all fixtures to run in a session-scoped loop by default:
5
5
6
-
.. code-block:: ini
7
-
:caption: pytest.ini
6
+
.. tabs::
8
7
9
-
[pytest]
10
-
asyncio_default_fixture_loop_scope = session
8
+
.. tab:: pytest.ini
11
9
12
-
.. code-block:: toml
13
-
:caption: pyproject.toml
10
+
.. code-block:: ini
14
11
15
-
[tool.pytest.ini_options]
16
-
asyncio_default_fixture_loop_scope = "session"
12
+
[pytest]
13
+
asyncio_default_fixture_loop_scope = session
17
14
18
-
.. code-block:: ini
19
-
:caption: setup.cfg
15
+
.. tab:: pyproject.toml
20
16
21
-
[tool:pytest]
22
-
asyncio_default_fixture_loop_scope = session
17
+
.. code-block:: toml
18
+
19
+
[tool.pytest.ini_options]
20
+
asyncio_default_fixture_loop_scope = "session"
21
+
22
+
.. tab:: setup.cfg
23
+
24
+
.. code-block:: ini
25
+
26
+
[tool:pytest]
27
+
asyncio_default_fixture_loop_scope = session
23
28
24
29
Please refer to :ref:`configuration/asyncio_default_fixture_loop_scope` for other valid scopes.
The :ref:`configuration/asyncio_default_test_loop_scope` configuration option sets the default event loop scope for asynchronous tests. The following code snippets configure all tests to run in a session-scoped loop by default:
5
5
6
-
.. code-block:: ini
7
-
:caption: pytest.ini
6
+
.. tabs::
8
7
9
-
[pytest]
10
-
asyncio_default_test_loop_scope = session
8
+
.. tab:: pytest.ini
11
9
12
-
.. code-block:: toml
13
-
:caption: pyproject.toml
10
+
.. code-block:: ini
14
11
15
-
[tool.pytest.ini_options]
16
-
asyncio_default_test_loop_scope = "session"
12
+
[pytest]
13
+
asyncio_default_test_loop_scope = session
17
14
18
-
.. code-block:: ini
19
-
:caption: setup.cfg
15
+
.. tab:: pyproject.toml
20
16
21
-
[tool:pytest]
22
-
asyncio_default_test_loop_scope = session
17
+
.. code-block:: toml
23
18
24
-
Please refer to :ref:`configuration/asyncio_default_test_loop_scope` for other valid scopes.
19
+
[tool.pytest.ini_options]
20
+
asyncio_default_test_loop_scope = "session"
21
+
22
+
.. tab:: setup.cfg
23
+
24
+
.. code-block:: ini
25
+
26
+
[tool:pytest]
27
+
asyncio_default_test_loop_scope = session
28
+
29
+
Please refer to :ref:`configuration/asyncio_default_test_loop_scope`
The ``pytest.mark.asyncio`` marker can be omitted entirely in |auto mode|_ where the *asyncio* marker is added automatically to *async* test functions.
21
27
22
-
By default, each test runs in it's own asyncio event loop.
28
+
By default, each test runs in its own asyncio event loop.
23
29
Multiple tests can share the same event loop by providing a *loop_scope* keyword argument to the *asyncio* mark.
24
-
The supported scopes are *function,* *class,* and *module,* *package,* and *session*.
25
-
The following code example provides a shared event loop for all tests in `TestClassScopedLoop`:
30
+
The supported scopes are *function*, *class*, *module*, *package*, and *session*.
31
+
32
+
The following code example provides a shared event loop for all tests in ``TestClassScopedLoop``:
0 commit comments