Skip to content

Commit 25f92ef

Browse files
author
Marco Paolini
committed
Use pytest-asyncio
1 parent 69681e8 commit 25f92ef

File tree

3 files changed

+12
-70
lines changed

3 files changed

+12
-70
lines changed

tests/conftest.py

+3-63
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,19 @@
1-
import asyncio
2-
import gc
31
from io import StringIO
42

53
import pytest
64

75

8-
# COPIED FROM aiohttp #
9-
10-
11-
@pytest.yield_fixture
12-
def loop(request):
13-
#loop = asyncio.get_event_loop()
14-
loop = asyncio.new_event_loop()
15-
asyncio.set_event_loop(None)
16-
#asyncio.set_event_loop(loop)
17-
18-
yield loop
19-
20-
is_closed = getattr(loop, 'is_closed')
21-
if is_closed is not None:
22-
closed = is_closed()
23-
else:
24-
closed = loop._closed
25-
if not closed:
26-
loop.call_soon(loop.stop)
27-
loop.run_forever()
28-
loop.close()
29-
gc.collect()
30-
asyncio.set_event_loop(None)
31-
32-
33-
@pytest.mark.tryfirst
34-
def pytest_pycollect_makeitem(collector, name, obj):
35-
if collector.funcnamefilter(name):
36-
if not callable(obj):
37-
return
38-
item = pytest.Function(name, parent=collector)
39-
if 'run_loop' in item.keywords:
40-
return list(collector._genfunctions(name, obj))
41-
42-
43-
@pytest.mark.tryfirst
44-
def pytest_pyfunc_call(pyfuncitem):
45-
"""
46-
Run asyncio marked test functions in an event loop instead of a normal
47-
function call.
48-
"""
49-
if 'run_loop' in pyfuncitem.keywords:
50-
funcargs = pyfuncitem.funcargs
51-
loop = funcargs['loop']
52-
testargs = {arg: funcargs[arg]
53-
for arg in pyfuncitem._fixtureinfo.argnames}
54-
loop.run_until_complete(pyfuncitem.obj(**testargs))
55-
return True
56-
57-
58-
def pytest_runtest_setup(item):
59-
if 'run_loop' in item.keywords and 'loop' not in item.fixturenames:
60-
# inject an event loop fixture for all async tests
61-
item.fixturenames.append('loop')
62-
63-
# end copy from aiohttp #
64-
65-
666
@pytest.yield_fixture
67-
def websocket_cli(loop):
7+
def websocket_cli(event_loop):
688

699
from pushpull.websocket.client import challenge
7010

7111
inp, out = StringIO(), StringIO()
72-
yield challenge('http://localhost:8080', 'test', inp, out, loop=loop), inp, out
12+
yield challenge('http://localhost:8080', 'test', inp, out, loop=event_loop), inp, out
7313

7414

7515
@pytest.yield_fixture
76-
def websocket_server(loop):
16+
def websocket_server(event_loop):
7717

7818
# TODO
7919
yield None

tests/test_cli.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
import pytest
66

77

8-
@pytest.mark.run_loop
9-
async def test_client_server_down(loop, websocket_cli):
8+
@pytest.mark.asyncio
9+
async def test_client_server_down(event_loop, websocket_cli):
1010
challenge, inp, out = websocket_cli
1111
with pytest.raises(aiohttp.errors.ClientOSError):
12-
await asyncio.wait_for(challenge, timeout=1, loop=loop)
12+
await asyncio.wait_for(challenge, timeout=1, loop=event_loop)
1313

1414

15+
@pytest.mark.asyncio
1516
@pytest.mark.skip(reason='TODO')
16-
@pytest.mark.run_loop
17-
async def test_client_server_send_one(loop, websocket_cli, websocket_server):
17+
async def test_client_server_send_one(event_loop, websocket_cli, websocket_server):
1818
challenge, inp, out = websocket_cli
1919
with mock.patch('pushpull.websocket.client.logger') as logger:
2020
inp.write('hey')
2121
inp.write('\n')
22-
await asyncio.wait_for(challenge, timeout=1, loop=loop)
22+
await asyncio.wait_for(challenge, timeout=1, loop=event_loop)
2323
assert logger.debug.n_calls == 1

tox.ini

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
envlist = py35
33

44
[testenv]
5-
deps=pytest
5+
deps =
6+
pytest
7+
pytest-asyncio==0.3.0
68
commands=py.test tests
79

0 commit comments

Comments
 (0)