Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for the async_generator library #62

Merged
merged 1 commit into from
Jul 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -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():
@@ -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
-------

13 changes: 5 additions & 8 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
@@ -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",
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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()