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 25, 2017
1 parent bc1a551 commit b2e7b50
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from aiohttp.pytest_plugin import LOOP_FACTORIES

pytest_plugins = 'pytester'

Expand Down Expand Up @@ -180,3 +181,87 @@ 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):
testdir.makepyfile("""\
import asyncio
import pytest
from unittest import mock
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
@asyncio.coroutine
def test_hello(cli):
resp = yield from cli.get('/')
assert resp.status == 200
""")
nb_loops = len(LOOP_FACTORIES)
result = testdir.runpytest('-p', 'no:sugar')
result.assert_outcomes(passed=1 * nb_loops)


@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 b2e7b50

Please sign in to comment.