Skip to content

Commit 3f4beb8

Browse files
committed
Merge branch 'master' into py38
2 parents 56e94d4 + d07cd2d commit 3f4beb8

26 files changed

+967
-144
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var/
2222
*.egg-info/
2323
.installed.cfg
2424
*.egg
25+
.hypothesis/
2526

2627
# PyInstaller
2728
# Usually these files are written by a python script from a template
@@ -55,3 +56,6 @@ docs/_build/
5556

5657
# PyBuilder
5758
target/
59+
60+
.venv*
61+
.idea

.travis.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ language: python
33
matrix:
44
include:
55
- python: 3.5
6-
env: TOXENV=py35
6+
env: TOX_ENV=py35
77
- python: 3.6
8-
env: TOXENV=py36
8+
env: TOX_ENV=py36
99
- python: 3.7
10-
env: TOXENV=py37
10+
env: TOX_ENV=py37
1111
- python: 3.8
12-
env: TOXENV=py38
12+
env: TOX_ENV=py38
1313

14-
install:
15-
- pip install tox
14+
install: pip install tox-travis coveralls
1615

1716
script: tox
1817

1918
after_success:
20-
- pip install coveralls && cd tests && coveralls
19+
- tox -e coverage-report
20+
- coveralls

README.rst

+186-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ pytest-asyncio: pytest support for asyncio
77
:target: https://travis-ci.org/pytest-dev/pytest-asyncio
88
.. image:: https://coveralls.io/repos/pytest-dev/pytest-asyncio/badge.svg
99
: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
1013

1114
pytest-asyncio is an Apache2 licensed library, written in Python, for testing
1215
asyncio code with pytest.
@@ -18,8 +21,8 @@ provides useful fixtures and markers to make testing easier.
1821
.. code-block:: python
1922
2023
@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()
2326
assert b'expected result' == res
2427
2528
pytest-asyncio has been strongly influenced by pytest-tornado_.
@@ -30,8 +33,10 @@ Features
3033
--------
3134

3235
- fixtures for creating and injecting versions of the asyncio event loop
36+
- fixtures for injecting unused tcp ports
3337
- 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
3540

3641
Installation
3742
------------
@@ -49,46 +54,210 @@ Fixtures
4954

5055
``event_loop``
5156
~~~~~~~~~~~~~~
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``).
5460

5561
Note that just using the ``event_loop`` fixture won't make your test function
5662
a coroutine. You'll need to interact with the event loop directly, using methods
5763
like ``event_loop.run_until_complete``. See the ``pytest.mark.asyncio`` marker
5864
for treating test functions like coroutines.
5965

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+
6070
.. code-block:: python
6171
6272
def test_http_client(event_loop):
6373
url = 'http://httpbin.org/get'
6474
resp = event_loop.run_until_complete(http_client(url))
6575
assert b'HTTP/1.1 200 OK' in resp
6676
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.
7293

7394
``unused_tcp_port``
7495
~~~~~~~~~~~~~~~~~~~
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
7697
binding temporary test servers.
7798

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+
78144
Markers
79145
-------
80146

81147
``pytest.mark.asyncio``
82-
~~~~~~~~~~~~~~~~~~~~~~~
148+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83149
Mark your test coroutine with this marker and pytest will execute it as an
84150
asyncio task using the event loop provided by the ``event_loop`` fixture. See
85151
the introductory section for an example.
86152

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.
92261

93262

94263
Contributing

pytest_asyncio/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
__version__ = '0.1.3'
1+
"""The main point for importing pytest-asyncio items."""
2+
__version__ = "0.11.0dev0"

0 commit comments

Comments
 (0)