Skip to content

Commit

Permalink
Improvement test_client can accept instance object. (#1083)
Browse files Browse the repository at this point in the history
* Improvement test_client that can accept instance object.

* Increase code test coverage.

* Change parameter from lambda to object instance.

* Add new testcase about testing test_client can accept object instance as parameter

* Add negative test case -- passing to  test_client application with wrong event loop.

* Fix bug in test case

* Check where args and kwargs are empty or not.
  • Loading branch information
ppiyakk2 authored and asvetlov committed Aug 16, 2016
1 parent 2269068 commit b109fdf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
12 changes: 11 additions & 1 deletion aiohttp/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 5 additions & 11 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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'})
Expand Down
36 changes: 35 additions & 1 deletion tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit b109fdf

Please sign in to comment.