@@ -39,6 +39,9 @@ Features
39
39
- pytest markers for treating tests as asyncio coroutines
40
40
- easy testing with non-default event loops
41
41
- 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 ``.
42
45
43
46
Installation
44
47
------------
@@ -51,6 +54,70 @@ To install pytest-asyncio, simply:
51
54
52
55
This is enough for pytest to pick up pytest-asyncio.
53
56
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
+
54
121
Fixtures
55
122
--------
56
123
@@ -116,16 +183,18 @@ Work just like their TCP counterparts but return unused UDP ports.
116
183
117
184
Async fixtures
118
185
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 `` .
120
187
121
188
.. code-block :: python3
122
189
123
- @pytest.fixture
190
+ import pytest_asyncio
191
+
192
+ @pytest_asyncio.fixture
124
193
async def async_gen_fixture():
125
194
await asyncio.sleep(0.1)
126
195
yield 'a value'
127
196
128
- @pytest .fixture(scope='module')
197
+ @pytest_asyncio .fixture(scope='module')
129
198
async def async_fixture():
130
199
return await asyncio.sleep(0.1)
131
200
@@ -134,6 +203,9 @@ to redefine the ``event_loop`` fixture to have the same or broader scope.
134
203
Async fixtures need the event loop, and so must have the same or narrower scope
135
204
than the ``event_loop `` fixture.
136
205
206
+ *auto * and *legacy * mode automatically converts async fixtures declared with the
207
+ standard ``@pytest.fixture `` decorator to *asyncio-driven * versions.
208
+
137
209
138
210
Markers
139
211
-------
@@ -164,6 +236,10 @@ Only test coroutines will be affected (by default, coroutines prefixed by
164
236
""" No marker!"""
165
237
await asyncio.sleep(0 , loop = event_loop)
166
238
239
+ In *auto * mode, the ``pytest.mark.asyncio `` marker can be omited, the merker is added
240
+ automatically to *async * test functions.
241
+
242
+
167
243
.. |pytestmark | replace :: ``pytestmark ``
168
244
.. _pytestmark : http://doc.pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules
169
245
0 commit comments