Skip to content

Commit

Permalink
added --fast tests option
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Kim committed Feb 15, 2017
1 parent 7cb42cf commit 2c15a3f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
23 changes: 19 additions & 4 deletions aiohttp/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,34 @@
from py import path

from aiohttp.web import Application
from aiohttp import test_utils

from .test_utils import unused_port as _unused_port
from .test_utils import (LOOP_FACTORIES, RawTestServer, TestClient, TestServer,
loop_context, setup_test_loop, teardown_test_loop)


def pytest_addoption(parser):
parser.addoption('--fast', action='store_true', default=False,
help='run tests faster by disabling extra checks')


@pytest.fixture
def fast(request):
""" --fast config option """
return request.config.getoption("--fast")


@contextlib.contextmanager
def _passthrough_loop_context(loop):
def _passthrough_loop_context(loop, fast=False):
if loop:
# loop already exists, pass it straight through
yield loop
else:
# this shadows loop_context's standard behavior
loop = setup_test_loop()
yield loop
teardown_test_loop(loop)
teardown_test_loop(loop, fast=fast)


def pytest_pycollect_makeitem(collector, name, obj):
Expand All @@ -36,9 +48,10 @@ def pytest_pyfunc_call(pyfuncitem):
"""
Run coroutines in an event loop instead of a normal function call.
"""
fast = pyfuncitem.config.getoption("--fast")
if asyncio.iscoroutinefunction(pyfuncitem.function):
existing_loop = pyfuncitem.funcargs.get('loop', None)
with _passthrough_loop_context(existing_loop) as _loop:
with _passthrough_loop_context(existing_loop, fast=fast) as _loop:
testargs = {arg: pyfuncitem.funcargs[arg]
for arg in pyfuncitem._fixtureinfo.argnames}

Expand All @@ -51,7 +64,9 @@ def pytest_pyfunc_call(pyfuncitem):
@pytest.yield_fixture(params=LOOP_FACTORIES)
def loop(request):
"""Return an instance of the event loop."""
with loop_context(request.param) as _loop:
fast = request.config.getoption("--fast")

with loop_context(request.param, fast=fast) as _loop:
_loop.set_debug(True)
yield _loop

Expand Down
22 changes: 9 additions & 13 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@
from .web import Application, Request, Server, UrlMappingMatchInfo


if bool(os.environ.get('AIOHTTP_TESTS_LIGHT')):
TESTS_LIGHT = True
else:
TESTS_LIGHT = False

if bool(os.environ.get('AIOHTTP_TESTS_USE_UVLOOP', True)):
TESTS_USE_UVLOOP = True
else:
TESTS_USE_UVLOOP = False
TESTS_FAST = bool(os.environ.get('AIOHTTP_TESTS_FAST'))
TESTS_USE_UVLOOP = not bool(os.environ.get('AIOHTTP_TESTS_USE_UVLOOP', True))


try:
Expand Down Expand Up @@ -444,14 +437,14 @@ def new_func(self):


@contextlib.contextmanager
def loop_context(loop_factory=asyncio.new_event_loop):
def loop_context(loop_factory=asyncio.new_event_loop, fast=None):
"""A contextmanager that creates an event_loop, for test purposes.
Handles the creation and cleanup of a test loop.
"""
loop = setup_test_loop(loop_factory)
yield loop
teardown_test_loop(loop)
teardown_test_loop(loop, fast=fast)


def setup_test_loop(loop_factory=asyncio.new_event_loop):
Expand All @@ -466,18 +459,21 @@ def setup_test_loop(loop_factory=asyncio.new_event_loop):
return loop


def teardown_test_loop(loop):
def teardown_test_loop(loop, fast=None):
"""Teardown and cleanup an event_loop created
by setup_test_loop.
"""
if fast is None:
fast = TESTS_FAST

closed = loop.is_closed()
if not closed:
loop.call_soon(loop.stop)
loop.run_forever()
loop.close()

if not TESTS_LIGHT:
if not fast:
gc.collect()

asyncio.set_event_loop(None)
Expand Down

0 comments on commit 2c15a3f

Please sign in to comment.