@@ -7,6 +7,9 @@ pytest-asyncio: pytest support for asyncio
7
7
:target: https://travis-ci.org/pytest-dev/pytest-asyncio
8
8
.. image :: https://coveralls.io/repos/pytest-dev/pytest-asyncio/badge.svg
9
9
:target: https://coveralls.io/r/pytest-dev/pytest-asyncio
10
+ .. image :: https://img.shields.io/pypi/pyversions/pytest-asyncio.svg
11
+ :target: https://github.com/pytest-dev/pytest-asyncio
12
+ :alt: Supported Python versions
10
13
11
14
pytest-asyncio is an Apache2 licensed library, written in Python, for testing
12
15
asyncio code with pytest.
@@ -18,8 +21,8 @@ provides useful fixtures and markers to make testing easier.
18
21
.. code-block :: python
19
22
20
23
@pytest.mark.asyncio
21
- def test_some_asyncio_code ()
22
- res = yield from library.do_something()
24
+ async def test_some_asyncio_code ():
25
+ res = await library.do_something()
23
26
assert b ' expected result' == res
24
27
25
28
pytest-asyncio has been strongly influenced by pytest-tornado _.
@@ -30,8 +33,10 @@ Features
30
33
--------
31
34
32
35
- fixtures for creating and injecting versions of the asyncio event loop
36
+ - fixtures for injecting unused tcp ports
33
37
- pytest markers for treating tests as asyncio coroutines
34
-
38
+ - easy testing with non-default event loops
39
+ - support for `async def ` fixtures and async generator fixtures
35
40
36
41
Installation
37
42
------------
@@ -49,46 +54,210 @@ Fixtures
49
54
50
55
``event_loop ``
51
56
~~~~~~~~~~~~~~
52
- Creates and injects a new instance of the default asyncio event loop. The loop
53
- will be closed at the end of the test.
57
+ Creates and injects a new instance of the default asyncio event loop. By
58
+ default, the loop will be closed at the end of the test (i.e. the default
59
+ fixture scope is ``function ``).
54
60
55
61
Note that just using the ``event_loop `` fixture won't make your test function
56
62
a coroutine. You'll need to interact with the event loop directly, using methods
57
63
like ``event_loop.run_until_complete ``. See the ``pytest.mark.asyncio `` marker
58
64
for treating test functions like coroutines.
59
65
66
+ Simply using this fixture will not set the generated event loop as the
67
+ default asyncio event loop, or change the asyncio event loop policy in any way.
68
+ Use ``pytest.mark.asyncio `` for this purpose.
69
+
60
70
.. code-block :: python
61
71
62
72
def test_http_client (event_loop ):
63
73
url = ' http://httpbin.org/get'
64
74
resp = event_loop.run_until_complete(http_client(url))
65
75
assert b ' HTTP/1.1 200 OK' in resp
66
76
67
- ``event_loop_process_pool ``
68
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
69
- The ``event_loop_process_pool `` fixture is almost identical to the
70
- ``event_loop `` fixture, except the created event loop will have a
71
- ``concurrent.futures.ProcessPoolExecutor `` set as the default executor.
77
+ This fixture can be easily overridden in any of the standard pytest locations
78
+ (e.g. directly in the test file, or in ``conftest.py ``) to use a non-default
79
+ event loop. This will take effect even if you're using the
80
+ ``pytest.mark.asyncio `` marker and not the ``event_loop `` fixture directly.
81
+
82
+ .. code-block :: python
83
+
84
+ @pytest.yield_fixture ()
85
+ def event_loop ():
86
+ loop = MyCustomLoop()
87
+ yield loop
88
+ loop.close()
89
+
90
+ If the ``pytest.mark.asyncio `` marker is applied, a pytest hook will
91
+ ensure the produced loop is set as the default global loop.
92
+ Fixtures depending on the ``event_loop `` fixture can expect the policy to be properly modified when they run.
72
93
73
94
``unused_tcp_port ``
74
95
~~~~~~~~~~~~~~~~~~~
75
- Finds and yields an unused TCP port on the localhost interface. Useful for
96
+ Finds and yields a single unused TCP port on the localhost interface. Useful for
76
97
binding temporary test servers.
77
98
99
+ ``unused_tcp_port_factory ``
100
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
101
+ A callable which returns a different unused TCP port each invocation. Useful
102
+ when several unused TCP ports are required in a test.
103
+
104
+ .. code-block :: python
105
+
106
+ def a_test (unused_tcp_port_factory ):
107
+ port1, port2 = unused_tcp_port_factory(), unused_tcp_port_factory()
108
+ ...
109
+
110
+ Async fixtures
111
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112
+ Asynchronous fixtures are defined just like ordinary pytest fixtures, except they should be coroutines or asynchronous generators.
113
+
114
+ .. code-block :: python3
115
+
116
+ @pytest.fixture
117
+ async def async_gen_fixture():
118
+ await asyncio.sleep(0.1)
119
+ yield 'a value'
120
+
121
+ @pytest.fixture(scope='module')
122
+ async def async_fixture():
123
+ return await asyncio.sleep(0.1)
124
+
125
+ All scopes are supported, but if you use a non-function scope you will need
126
+ to redefine the ``event_loop `` fixture to have the same or broader scope.
127
+ Async fixtures need the event loop, and so must have the same or narrower scope
128
+ than the ``event_loop `` fixture.
129
+
130
+ If you want to do this with Python 3.5, the ``yield `` statement must be replaced with ``await yield_() `` and the coroutine
131
+ function must be decorated with ``@async_generator ``, like so:
132
+
133
+ .. code-block :: python3
134
+
135
+ from async_generator import yield_, async_generator
136
+
137
+ @pytest.fixture
138
+ @async_generator
139
+ async def async_gen_fixture():
140
+ await asyncio.sleep(0.1)
141
+ await yield_('a value')
142
+
143
+
78
144
Markers
79
145
-------
80
146
81
147
``pytest.mark.asyncio ``
82
- ~~~~~~~~~~~~~~~~~~~~~~~
148
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83
149
Mark your test coroutine with this marker and pytest will execute it as an
84
150
asyncio task using the event loop provided by the ``event_loop `` fixture. See
85
151
the introductory section for an example.
86
152
87
- ``pytest.mark.asyncio_process_pool ``
88
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89
- The ``asyncio_process_pool `` marker is almost identical to the ``asyncio ``
90
- marker, except the event loop used will have a
91
- ``concurrent.futures.ProcessPoolExecutor `` set as the default executor.
153
+ The event loop used can be overriden by overriding the ``event_loop `` fixture
154
+ (see above).
155
+
156
+ In order to make your test code a little more concise, the pytest |pytestmark |_
157
+ feature can be used to mark entire modules or classes with this marker.
158
+ Only test coroutines will be affected (by default, coroutines prefixed by
159
+ ``test_ ``), so, for example, fixtures are safe to define.
160
+
161
+ .. code-block :: python
162
+
163
+ import asyncio
164
+ import pytest
165
+
166
+ # All test coroutines will be treated as marked.
167
+ pytestmark = pytest.mark.asyncio
168
+
169
+ async def test_example (event_loop ):
170
+ """ No marker!"""
171
+ await asyncio.sleep(0 , loop = event_loop)
172
+
173
+ .. |pytestmark | replace :: ``pytestmark ``
174
+ .. _pytestmark : http://doc.pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules
175
+
176
+ Changelog
177
+ ---------
178
+
179
+ 0.11.0 (UNRELEASED)
180
+ ~~~~~~~~~~~~~~~~~~~
181
+
182
+ 0.10.0 (2019-01-08)
183
+ ~~~~~~~~~~~~~~~~~~~~
184
+ - ``pytest-asyncio `` integrates with `Hypothesis <https://hypothesis.readthedocs.io >`_
185
+ to support ``@given `` on async test functions using ``asyncio ``.
186
+ `#102 ` <https://github.com/pytest-dev/pytest-asyncio/pull/102>
187
+ - Pytest 4.1 support.
188
+ `#105 ` <https://github.com/pytest-dev/pytest-asyncio/pull/105>
189
+
190
+ 0.9.0 (2018-07-28)
191
+ ~~~~~~~~~~~~~~~~~~
192
+ - Python 3.7 support.
193
+ - Remove ``event_loop_process_pool `` fixture and
194
+ ``pytest.mark.asyncio_process_pool `` marker (see
195
+ https://bugs.python.org/issue34075 for deprecation and removal details)
196
+
197
+ 0.8.0 (2017-09-23)
198
+ ~~~~~~~~~~~~~~~~~~
199
+ - Improve integration with other packages (like aiohttp) with more careful event loop handling.
200
+ `#64 ` <https://github.com/pytest-dev/pytest-asyncio/pull/64>
201
+
202
+ 0.7.0 (2017-09-08)
203
+ ~~~~~~~~~~~~~~~~~~
204
+ - Python versions pre-3.6 can use the async_generator library for async fixtures.
205
+ `#62 <https://github.com/pytest-dev/pytest-asyncio/pull/62> `
206
+
207
+
208
+ 0.6.0 (2017-05-28)
209
+ ~~~~~~~~~~~~~~~~~~
210
+ - Support for Python versions pre-3.5 has been dropped.
211
+ - ``pytestmark `` now works on both module and class level.
212
+ - The ``forbid_global_loop `` parameter has been removed.
213
+ - Support for async and async gen fixtures has been added.
214
+ `#45 <https://github.com/pytest-dev/pytest-asyncio/pull/45 >`_
215
+ - The deprecation warning regarding ``asyncio.async() `` has been fixed.
216
+ `#51 <https://github.com/pytest-dev/pytest-asyncio/pull/51 >`_
217
+
218
+ 0.5.0 (2016-09-07)
219
+ ~~~~~~~~~~~~~~~~~~
220
+ - Introduced a changelog.
221
+ `#31 <https://github.com/pytest-dev/pytest-asyncio/issues/31 >`_
222
+ - The ``event_loop `` fixture is again responsible for closing itself.
223
+ This makes the fixture slightly harder to correctly override, but enables
224
+ other fixtures to depend on it correctly.
225
+ `#30 <https://github.com/pytest-dev/pytest-asyncio/issues/30 >`_
226
+ - Deal with the event loop policy by wrapping a special pytest hook,
227
+ ``pytest_fixture_setup ``. This allows setting the policy before fixtures
228
+ dependent on the ``event_loop `` fixture run, thus allowing them to take
229
+ advantage of the ``forbid_global_loop `` parameter. As a consequence of this,
230
+ we now depend on pytest 3.0.
231
+ `#29 <https://github.com/pytest-dev/pytest-asyncio/issues/29 >`_
232
+
233
+
234
+ 0.4.1 (2016-06-01)
235
+ ~~~~~~~~~~~~~~~~~~
236
+ - Fix a bug preventing the propagation of exceptions from the plugin.
237
+ `#25 <https://github.com/pytest-dev/pytest-asyncio/issues/25 >`_
238
+
239
+ 0.4.0 (2016-05-30)
240
+ ~~~~~~~~~~~~~~~~~~
241
+ - Make ``event_loop `` fixtures simpler to override by closing them in the
242
+ plugin, instead of directly in the fixture.
243
+ `#21 <https://github.com/pytest-dev/pytest-asyncio/pull/21 >`_
244
+ - Introduce the ``forbid_global_loop `` parameter.
245
+ `#21 <https://github.com/pytest-dev/pytest-asyncio/pull/21 >`_
246
+
247
+ 0.3.0 (2015-12-19)
248
+ ~~~~~~~~~~~~~~~~~~
249
+ - Support for Python 3.5 ``async ``/``await `` syntax.
250
+ `#17 <https://github.com/pytest-dev/pytest-asyncio/pull/17 >`_
251
+
252
+ 0.2.0 (2015-08-01)
253
+ ~~~~~~~~~~~~~~~~~~
254
+ - ``unused_tcp_port_factory `` fixture.
255
+ `#10 <https://github.com/pytest-dev/pytest-asyncio/issues/10 >`_
256
+
257
+
258
+ 0.1.1 (2015-04-23)
259
+ ~~~~~~~~~~~~~~~~~~
260
+ Initial release.
92
261
93
262
94
263
Contributing
0 commit comments