-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And cleanup pending tasks before exiting tests
- Loading branch information
Showing
3 changed files
with
52 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
flake8==3.2.1 | ||
ipdb==0.10.1 | ||
pytest==3.0.5 | ||
pytest-asyncio==0.5.0 | ||
pytest-cov==2.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,7 @@ | ||
import asyncio | ||
import gc | ||
import socket | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="session", params=[True, False], | ||
ids=['debug:true', 'debug:false']) | ||
def debug(request): | ||
return request.param | ||
|
||
|
||
@pytest.yield_fixture | ||
def loop(request, debug): | ||
old_loop = asyncio.get_event_loop() | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(None) | ||
loop.set_debug(debug) | ||
yield loop | ||
|
||
if not loop._closed: | ||
loop.call_soon(loop.stop) | ||
loop.run_forever() | ||
loop.close() | ||
gc.collect() | ||
asyncio.set_event_loop(old_loop) | ||
|
||
|
||
@pytest.mark.tryfirst | ||
def pytest_pycollect_makeitem(collector, name, obj): | ||
if collector.funcnamefilter(name): | ||
item = pytest.Function(name, parent=collector) | ||
if 'run_loop' in item.keywords: | ||
return list(collector._genfunctions(name, obj)) | ||
|
||
|
||
@pytest.mark.tryfirst | ||
def pytest_pyfunc_call(pyfuncitem): | ||
""" | ||
Run asyncio marked test functions in an event loop instead of a normal | ||
function call. | ||
""" | ||
if 'run_loop' in pyfuncitem.keywords: | ||
funcargs = pyfuncitem.funcargs | ||
loop = funcargs['loop'] | ||
testargs = {arg: funcargs[arg] | ||
for arg in pyfuncitem._fixtureinfo.argnames} | ||
|
||
if not asyncio.iscoroutinefunction(pyfuncitem.obj): | ||
func = asyncio.coroutine(pyfuncitem.obj) | ||
else: | ||
func = pyfuncitem.obj | ||
loop.run_until_complete(func(**testargs)) | ||
return True | ||
|
||
|
||
def pytest_runtest_setup(item): | ||
if 'run_loop' in item.keywords and 'loop' not in item.fixturenames: | ||
# inject an event loop fixture for all async tests | ||
item.fixturenames.append('loop') | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def unused_port(): | ||
def f(): | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
s.bind(('127.0.0.1', 0)) | ||
return s.getsockname()[1] | ||
return f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters