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
pytest-asyncio is an Apache2 licensed library, written in Python, for testing
17
-
asyncio code with pytest.
16
+
pytest-asyncio is a `pytest <https://docs.pytest.org/en/latest/contents.html>`_ plugin. It facilitates testing of code that uses the `asyncio <https://docs.python.org/3/library/asyncio.html>`_ library.
18
17
19
-
asyncio code is usually written in the form of coroutines, which makes it
20
-
slightly more difficult to test using normal testing tools. pytest-asyncio
21
-
provides useful fixtures and markers to make testing easier.
18
+
Specifically, pytest-asyncio provides support for coroutines as test functions. This allows users to *await* code inside their tests. For example, the following code is executed as a test item by pytest:
22
19
23
20
.. code-block:: python
24
21
@@ -27,21 +24,14 @@ provides useful fixtures and markers to make testing easier.
27
24
res =await library.do_something()
28
25
assertb"expected result"== res
29
26
30
-
pytest-asyncio has been strongly influenced by pytest-tornado_.
The value can be overridden by command-line option for ``pytest`` invocation:
72
-
73
-
.. code-block:: bash
74
-
75
-
$ pytest tests --asyncio-mode=strict
76
-
77
-
Auto mode
78
-
~~~~~~~~~
79
-
80
-
When the mode is auto, all discovered *async* tests are considered *asyncio-driven* even
81
-
if they have no ``@pytest.mark.asyncio`` marker.
82
-
83
-
All async fixtures are considered *asyncio-driven* as well, even if they are decorated
84
-
with a regular ``@pytest.fixture`` decorator instead of dedicated
85
-
``@pytest_asyncio.fixture`` counterpart.
86
-
87
-
*asyncio-driven* means that tests and fixtures are executed by ``pytest-asyncio``
88
-
plugin.
89
-
90
-
This mode requires the simplest tests and fixtures configuration and is
91
-
recommended for default usage *unless* the same project and its test suite should
92
-
execute tests from different async frameworks, e.g. ``asyncio`` and ``trio``. In this
93
-
case, auto-handling can break tests designed for other framework; please use *strict*
94
-
mode instead.
95
-
96
-
Strict mode
97
-
~~~~~~~~~~~
98
-
99
-
Strict mode enforces ``@pytest.mark.asyncio`` and ``@pytest_asyncio.fixture`` usage.
100
-
Without these markers, tests and fixtures are not considered as *asyncio-driven*, other
101
-
pytest plugin can handle them.
102
-
103
-
Please use this mode if multiple async frameworks should be combined in the same test
104
-
suite.
105
-
106
-
This mode is used by default for the sake of project inter-compatibility.
107
-
108
-
109
-
Fixtures
110
-
--------
111
-
112
-
``event_loop``
113
-
~~~~~~~~~~~~~~
114
-
Creates a new asyncio event loop based on the current event loop policy. The new loop
115
-
is available as the return value of this fixture or via `asyncio.get_running_loop <https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_running_loop>`__.
116
-
The event loop is closed when the fixture scope ends. The fixture scope defaults
117
-
to ``function`` scope.
118
-
119
-
Note that just using the ``event_loop`` fixture won't make your test function
120
-
a coroutine. You'll need to interact with the event loop directly, using methods
121
-
like ``event_loop.run_until_complete``. See the ``pytest.mark.asyncio`` marker
pytest-asyncio runs each test item in its own asyncio event loop. The loop can be accessed via the ``event_loop`` fixture, which is automatically requested by all async tests.
You can think of `event_loop` as an autouse fixture for async tests.
15
+
16
+
Test discovery modes
17
+
====================
18
+
19
+
Pytest-asyncio provides two modes for test discovery, *strict* and *auto*.
20
+
21
+
22
+
Strict mode
23
+
-----------
24
+
25
+
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.
26
+
27
+
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.
28
+
29
+
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.
30
+
31
+
Auto mode
32
+
---------
33
+
34
+
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``.
35
+
36
+
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.
37
+
38
+
If you intend to support multiple asynchronous programming libraries, e.g. *asyncio* and *trio*, strict mode will be the preferred option.
pytest-asyncio is a `pytest <https://docs.pytest.org/en/latest/contents.html>`_ plugin. It facilitates testing of code that uses the `asyncio <https://docs.python.org/3/library/asyncio.html>`_ library.
14
+
15
+
Specifically, pytest-asyncio provides support for coroutines as test functions. This allows users to *await* code inside their tests. For example, the following code is executed as a test item by pytest:
16
+
17
+
.. code-block:: python
18
+
19
+
@pytest.mark.asyncio
20
+
asyncdeftest_some_asyncio_code():
21
+
res =await library.do_something()
22
+
assertb"expected result"== res
23
+
24
+
25
+
Note that test classes subclassing the standard `unittest <https://docs.python.org/3/library/unittest.html>`__ library are not supported. Users
26
+
are advised to use `unittest.IsolatedAsyncioTestCase <https://docs.python.org/3/library/unittest.html#unittest.IsolatedAsyncioTestCase>`__
27
+
or an async framework such as `asynctest <https://asynctest.readthedocs.io/en/latest>`__.
28
+
29
+
30
+
pytest-asyncio is available under the `Apache License 2.0 <https://github.com/pytest-dev/pytest-asyncio/blob/master/LICENSE>`_.
0 commit comments