diff --git a/aiohttp/pytest_plugin.py b/aiohttp/pytest_plugin.py index 0ac3f87d5e4..9d1151dc9a4 100644 --- a/aiohttp/pytest_plugin.py +++ b/aiohttp/pytest_plugin.py @@ -2,6 +2,7 @@ import contextlib import pytest +from aiohttp.web import Application from .test_utils import (TestClient, loop_context, setup_test_loop, teardown_test_loop) @@ -55,7 +56,16 @@ def test_client(loop): @asyncio.coroutine def _create_from_app_factory(app_factory, *args, **kwargs): - app = app_factory(loop, *args, **kwargs) + if not isinstance(app_factory, Application): + app = app_factory(loop, *args, **kwargs) + else: + assert not args, "args should be empty" + assert not kwargs, "kwargs should be empty" + app = app_factory + + assert app.loop is loop, \ + "Application is attached to other event loop" + client = TestClient(app) yield from client.start_server() clients.append(client) diff --git a/docs/testing.rst b/docs/testing.rst index 9cf452bd970..14c2a0ff5ae 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -40,13 +40,10 @@ A simple would be:: async def hello(request): return web.Response(body=b'Hello, world') - def create_app(loop): + async def test_hello(test_client, loop): app = web.Application(loop=loop) app.router.add_get('/', hello) - return app - - async def test_hello(test_client): - client = await test_client(create_app) + client = await test_client(app) resp = await client.get('/') assert resp.status == 200 text = await resp.text() @@ -69,14 +66,11 @@ app test client:: return web.Response( body='value: {}'.format(request.app['value']).encode()) - def create_app(loop): - app = web.Application(loop=loop) - app.router.add_route('*', '/', previous) - return app - @pytest.fixture def cli(loop, test_client): - return loop.run_until_complete(test_client(create_app)) + app = web.Application(loop=loop) + app.router.add_get('/', hello) + return loop.run_until_complete(test_client(app)) async def test_set_value(cli): resp = await cli.post('/', data={'value': 'foo'}) diff --git a/tests/test_pytest_plugin.py b/tests/test_pytest_plugin.py index 404918d38b6..4a038eb7590 100644 --- a/tests/test_pytest_plugin.py +++ b/tests/test_pytest_plugin.py @@ -5,6 +5,8 @@ def test_myplugin(testdir): testdir.makepyfile("""\ import asyncio import pytest +from unittest import mock + from aiohttp import web pytest_plugins = 'aiohttp.pytest_plugin' @@ -30,6 +32,17 @@ def test_hello(test_client): assert 'Hello, world' in text +@asyncio.coroutine +def test_hello_from_app(test_client, loop): + app = web.Application(loop=loop) + app.router.add_get('/', hello) + client = yield from test_client(app) + resp = yield from client.get('/') + assert resp.status == 200 + text = yield from resp.text() + assert 'Hello, world' in text + + @asyncio.coroutine def test_hello_with_loop(test_client, loop): client = yield from test_client(create_app) @@ -48,6 +61,27 @@ def test_hello_fails(test_client): assert 'Hello, wield' in text +@asyncio.coroutine +def test_hello_with_fake_loop(test_client): + with pytest.raises(AssertionError): + fake_loop = mock.Mock() + yield from test_client(web.Application(loop=fake_loop)) + + +@asyncio.coroutine +def test_set_args(test_client, loop): + with pytest.raises(AssertionError): + app = web.Application(loop=loop) + yield from test_client(app, 1, 2, 3) + + +@asyncio.coroutine +def test_set_keyword_args(test_client, loop): + with pytest.raises(AssertionError): + app = web.Application(loop=loop) + yield from test_client(app, param=1) + + @asyncio.coroutine def test_noop(): pass @@ -111,4 +145,4 @@ def make_app(loop): """) result = testdir.runpytest('-p', 'no:sugar') - result.assert_outcomes(passed=7, failed=1) + result.assert_outcomes(passed=11, failed=1)