Skip to content

Commit 2523772

Browse files
committed
Update README
1 parent da87ba6 commit 2523772

File tree

1 file changed

+79
-3
lines changed

1 file changed

+79
-3
lines changed

README.rst

+79-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Features
3939
- pytest markers for treating tests as asyncio coroutines
4040
- easy testing with non-default event loops
4141
- support for `async def` fixtures and async generator fixtures
42+
- support *auto* mode to handle all async fixtures and tests automatically by asyncio;
43+
provide *strict* mode if a test suite should work with different async frameworks
44+
simultaneously, e.g. ``asyncio`` and ``trio``.
4245

4346
Installation
4447
------------
@@ -51,6 +54,70 @@ To install pytest-asyncio, simply:
5154
5255
This is enough for pytest to pick up pytest-asyncio.
5356

57+
Modes
58+
-----
59+
60+
Strting from ``pytest-asyncio>=0.17``, three modes are provided: *auto*, *strict* and
61+
*legacy* (deault).
62+
63+
The mode can be set by ``asyncio_mode`` configuration option in `configuration file
64+
<https://docs.pytest.org/en/latest/reference/customize.html>`_:
65+
66+
.. code-block:: ini
67+
68+
# pytest.ini
69+
[pytest]
70+
asyncio_mode = auto
71+
72+
The value can be overriden by command-line option for ``pytest`` invocation:
73+
74+
.. code-block:: bash
75+
76+
$ pytest tests --asyncio-mode=strict
77+
78+
Auto mode
79+
~~~~~~~~~
80+
81+
When the mode is auto, all discovered *async* tests are considered *asyncio-driven* even
82+
if they have no ``@pytest.mark.asyncio`` marker.
83+
84+
All async fixtures are considered *asyncio-driven* as well, even if they are decorated
85+
with a regular ``@pytest.fixture`` decorator instead of dedicated
86+
``@pytest_asyncio.fixture`` counterpart.
87+
88+
*asyncio-driven* means that tests and fixtures are executed by ``pytest-asyncio``
89+
plugin.
90+
91+
This mode requires the simpliest tests and fixtures configuration and is
92+
recommended for default usage *unless* the same project and its test suite should
93+
execute tests from different async frameworks, e.g. ``asyncio`` and ``trio``. In this
94+
case, auto-handling can break tests designed for other framework; plase use *strict*
95+
mode instead.
96+
97+
Strict mode
98+
~~~~~~~~~~~
99+
100+
Strict mode enforces ``@pytest.mark.asyncio`` and ``@pytest_asyncio.fixture`` usage.
101+
Without these markers, tests and fixtures are not considered as *asyncio-driven*, other
102+
pytest plugin can handle them.
103+
104+
Please use this mode if multiple async frameworks should be combined in the same test
105+
suite.
106+
107+
108+
Legacy mode
109+
~~~~~~~~~~~
110+
111+
This mode follows rules used by ``pytest-asyncio<0.17``: tests are not auto-marked but
112+
fixtures are.
113+
114+
This mode is used by default for the sake of backward compatibility, deprecation
115+
warnings are emitted with suggestion to either switching to ``auto`` mode or using
116+
``strict`` mode with ``@pytest_asyncio.fixture`` decorators.
117+
118+
In future, the default will be changed.
119+
120+
54121
Fixtures
55122
--------
56123

@@ -116,16 +183,18 @@ Work just like their TCP counterparts but return unused UDP ports.
116183

117184
Async fixtures
118185
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119-
Asynchronous fixtures are defined just like ordinary pytest fixtures, except they should be coroutines or asynchronous generators.
186+
Asynchronous fixtures are defined just like ordinary pytest fixtures, except they should be decorated with ``@pytest_asyncio.fixture``.
120187

121188
.. code-block:: python3
122189
123-
@pytest.fixture
190+
import pytest_asyncio
191+
192+
@pytest_asyncio.fixture
124193
async def async_gen_fixture():
125194
await asyncio.sleep(0.1)
126195
yield 'a value'
127196
128-
@pytest.fixture(scope='module')
197+
@pytest_asyncio.fixture(scope='module')
129198
async def async_fixture():
130199
return await asyncio.sleep(0.1)
131200
@@ -134,6 +203,9 @@ to redefine the ``event_loop`` fixture to have the same or broader scope.
134203
Async fixtures need the event loop, and so must have the same or narrower scope
135204
than the ``event_loop`` fixture.
136205

206+
*auto* and *legacy* mode automatically converts async fixtures declared with the
207+
standard ``@pytest.fixture`` decorator to *asyncio-driven* versions.
208+
137209

138210
Markers
139211
-------
@@ -164,6 +236,10 @@ Only test coroutines will be affected (by default, coroutines prefixed by
164236
"""No marker!"""
165237
await asyncio.sleep(0, loop=event_loop)
166238
239+
In *auto* mode, the ``pytest.mark.asyncio`` marker can be omited, the merker is added
240+
automatically to *async* test functions.
241+
242+
167243
.. |pytestmark| replace:: ``pytestmark``
168244
.. _pytestmark: http://doc.pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules
169245

0 commit comments

Comments
 (0)