-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Switch pytest-asyncio #14
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave it as it is, I added pyup (https://pyup.io/) bot to this repo we will have automatic PR with dependency update, see example here aio-libs/aiomonitor#10 Other aio-libs do the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok