Skip to content

Commit

Permalink
Add tests for async fixtures (aio-libs#2223)
Browse files Browse the repository at this point in the history
  • Loading branch information
k4nar committed Aug 28, 2017
1 parent 6b55c79 commit e9a8fcb
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest

from aiohttp.pytest_plugin import LOOP_FACTORIES


pytest_plugins = 'pytester'

Expand Down Expand Up @@ -180,3 +182,110 @@ async def test_bad():
stdout, _ = capsys.readouterr()
assert ("test_warning_checks.py:__LINE__:coroutine 'foobar' was "
"never awaited" in re.sub('\d{2,}', '__LINE__', stdout))


def test_aiohttp_plugin_async_fixture(testdir, capsys):
testdir.makepyfile("""\
import asyncio
import pytest
from aiohttp import web
pytest_plugins = 'aiohttp.pytest_plugin'
@asyncio.coroutine
def hello(request):
return web.Response(body=b'Hello, world')
def create_app(loop):
app = web.Application()
app.router.add_route('GET', '/', hello)
return app
@pytest.fixture
@asyncio.coroutine
def cli(test_client):
client = yield from test_client(create_app)
return client
@pytest.fixture
@asyncio.coroutine
def foo():
return 42
@asyncio.coroutine
def test_hello(cli):
resp = yield from cli.get('/')
assert resp.status == 200
def test_foo(loop, foo):
assert foo == 42
def test_foo_with_request(loop, request, foo):
assert foo == 42
assert request.function == test_foo_with_request
def test_foo_without_loop(foo):
# will raise an error because there is no loop
pass
""")
nb_loops = len(LOOP_FACTORIES)
result = testdir.runpytest('-p', 'no:sugar')
result.assert_outcomes(passed=3 * nb_loops, error=1)
result.stdout.fnmatch_lines(
"*Asynchronous fixtures must depend on the 'loop' fixture "
"or be used in tests depending from it."
)


@pytest.mark.skipif(sys.version_info < (3, 6), reason='old python')
def test_aiohttp_plugin_async_gen_fixture(testdir):
testdir.makepyfile("""\
import asyncio
import pytest
from unittest import mock
from aiohttp import web
pytest_plugins = 'aiohttp.pytest_plugin'
canary = mock.Mock()
async def hello(request):
return web.Response(body=b'Hello, world')
def create_app(loop):
app = web.Application()
app.router.add_route('GET', '/', hello)
return app
@pytest.fixture
async def cli(test_client):
yield await test_client(create_app)
canary()
async def test_hello(cli):
resp = await cli.get('/')
assert resp.status == 200
def test_finalized():
assert canary.called is True
""")
nb_loops = len(LOOP_FACTORIES)
result = testdir.runpytest('-p', 'no:sugar')
result.assert_outcomes(passed=1 * nb_loops + 1)

0 comments on commit e9a8fcb

Please sign in to comment.