Skip to content

Commit

Permalink
Merge pull request #62 from agronholm/async_generator
Browse files Browse the repository at this point in the history
Added support for the async_generator library
  • Loading branch information
Tinche committed Jul 9, 2017
2 parents 1ec9287 + 96f931a commit 3944bc4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
16 changes: 15 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Async fixtures
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Asynchronous fixtures are defined just like ordinary pytest fixtures, except they should be coroutines or asynchronous generators.

.. code-block:: python
.. code-block:: python3
@pytest.fixture
async def async_gen_fixture():
Expand All @@ -130,6 +130,20 @@ to redefine the ``event_loop`` fixture to have the same or broader scope.
Async fixtures need the event loop, and so must have the same or narrower scope
than the ``event_loop`` fixture.

If you want to do this with Python 3.5, the ``yield`` statement must be replaced with ``await yield_()`` and the coroutine
function must be decorated with ``@async_generator``, like so:

.. code-block:: python3
from async_generator import yield_, async_generator
@pytest.fixture
@async_generator
async def async_gen_fixture():
await asyncio.sleep(0.1)
await yield_('a value')
Markers
-------

Expand Down
13 changes: 5 additions & 8 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@
import contextlib
import inspect
import socket
import sys
from concurrent.futures import ProcessPoolExecutor

import pytest
from _pytest.python import transfer_markers

try:
from async_generator import isasyncgenfunction
except ImportError:
from inspect import isasyncgenfunction


def _is_coroutine(obj):
"""Check to see if an object is really an asyncio coroutine."""
return asyncio.iscoroutinefunction(obj) or inspect.isgeneratorfunction(obj)


if sys.version_info[:2] < (3, 6):
def isasyncgenfunction(_):
return False
else:
from inspect import isasyncgenfunction


def pytest_configure(config):
"""Inject documentation."""
config.addinivalue_line("markers",
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def find_version(*file_paths):
install_requires=[
'pytest >= 3.0.6',
],
extras_require={
':python_version == "3.5"': 'async_generator >= 1.3'
},
entry_points={
'pytest11': ['asyncio = pytest_asyncio.plugin'],
},
}
)
3 changes: 2 additions & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coverage==4.1
tox==2.5.0
tox==2.5.0
async_generator==1.8
40 changes: 40 additions & 0 deletions tests/async_fixtures/test_async_gen_fixtures_35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest.mock

import pytest
from async_generator import yield_, async_generator

START = object()
END = object()
RETVAL = object()


@pytest.fixture(scope='module')
def mock():
return unittest.mock.Mock(return_value=RETVAL)


@pytest.fixture
@async_generator
async def async_gen_fixture(mock):
try:
await yield_(mock(START))
except Exception as e:
mock(e)
else:
mock(END)


@pytest.mark.asyncio
async def test_async_gen_fixture(async_gen_fixture, mock):
assert mock.called
assert mock.call_args_list[-1] == unittest.mock.call(START)
assert async_gen_fixture is RETVAL


@pytest.mark.asyncio
async def test_async_gen_fixture_finalized(mock):
try:
assert mock.called
assert mock.call_args_list[-1] == unittest.mock.call(END)
finally:
mock.reset_mock()

0 comments on commit 3944bc4

Please sign in to comment.