Skip to content

Commit 71856a2

Browse files
committed
Merge pull request #9 from asvetlov/no_external_sites
Get rid of using external resource in tests
2 parents 2afbebc + 7dbf830 commit 71856a2

File tree

1 file changed

+21
-41
lines changed

1 file changed

+21
-41
lines changed

tests/test_simple.py

+21-41
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,29 @@
11
"""Quick'n'dirty unit tests for provided fixtures and markers."""
22
import asyncio
33
import os
4-
import urllib
54
import pytest
65

76

87
@asyncio.coroutine
9-
def simple_http_client(url):
10-
"""Just a simple http client, for testing."""
11-
u = urllib.parse.urlparse(url)
12-
port = u.port if u.port else 80
13-
r, w = yield from asyncio.open_connection(host=u.netloc, port=port)
14-
w.write(b'GET ' + u.path.encode() + b' HTTP/1.0\r\n')
15-
w.write(b'Host: ' + u.netloc.encode() + b'\r\n')
16-
w.write(b'\r\n')
17-
yield from w.drain()
18-
19-
resp = yield from r.read()
20-
21-
w.close()
22-
return resp
8+
def async_coro(loop):
9+
yield from asyncio.sleep(0, loop=loop)
10+
return 'ok'
2311

2412

2513
def test_event_loop_fixture(event_loop):
2614
"""Test the injection of the event_loop fixture."""
2715
assert event_loop
28-
url = 'http://httpbin.org/get'
29-
resp = event_loop.run_until_complete(simple_http_client(url))
30-
assert b'HTTP/1.1 200 OK' in resp
16+
ret = event_loop.run_until_complete(async_coro(event_loop))
17+
assert ret == 'ok'
3118

3219

3320
def test_event_loop_processpool_fixture(event_loop_process_pool):
3421
"""Test the injection of the event_loop with a process pool fixture."""
3522
assert event_loop_process_pool
36-
url = 'http://httpbin.org/get'
37-
resp = event_loop_process_pool.run_until_complete(simple_http_client(url))
38-
assert b'HTTP/1.1 200 OK' in resp
23+
24+
ret = event_loop_process_pool.run_until_complete(
25+
async_coro(event_loop_process_pool))
26+
assert ret == 'ok'
3927

4028
this_pid = os.getpid()
4129
future = event_loop_process_pool.run_in_executor(None, os.getpid)
@@ -46,45 +34,38 @@ def test_event_loop_processpool_fixture(event_loop_process_pool):
4634
@pytest.mark.asyncio
4735
def test_asyncio_marker():
4836
"""Test the asyncio pytest marker."""
49-
url = 'http://httpbin.org/get'
50-
resp = yield from simple_http_client(url)
51-
assert b'HTTP/1.1 200 OK' in resp
37+
yield # sleep(0)
5238

5339

5440
@pytest.mark.asyncio
5541
def test_asyncio_marker_with_default_param(a_param=None):
5642
"""Test the asyncio pytest marker."""
57-
url = 'http://httpbin.org/get'
58-
resp = yield from simple_http_client(url)
59-
assert b'HTTP/1.1 200 OK' in resp
43+
yield # sleep(0)
6044

6145

6246
@pytest.mark.asyncio_process_pool
6347
def test_asyncio_process_pool_marker(event_loop):
6448
"""Test the asyncio pytest marker."""
65-
url = 'http://httpbin.org/get'
66-
resp = yield from simple_http_client(url)
67-
assert b'HTTP/1.1 200 OK' in resp
68-
69-
this_pid = os.getpid()
70-
pool_pid = yield from event_loop.run_in_executor(None, os.getpid)
71-
assert this_pid != pool_pid
49+
ret = yield from async_coro(event_loop)
50+
assert ret == 'ok'
7251

7352

7453
@pytest.mark.asyncio
75-
def test_unused_port_fixture(unused_tcp_port):
54+
def test_unused_port_fixture(unused_tcp_port, event_loop):
7655
"""Test the unused TCP port fixture."""
7756

7857
@asyncio.coroutine
7958
def closer(_, writer):
8059
writer.close()
8160

8261
server1 = yield from asyncio.start_server(closer, host='localhost',
83-
port=unused_tcp_port)
62+
port=unused_tcp_port,
63+
loop=event_loop)
8464

8565
with pytest.raises(IOError):
8666
yield from asyncio.start_server(closer, host='localhost',
87-
port=unused_tcp_port)
67+
port=unused_tcp_port,
68+
loop=event_loop)
8869

8970
server1.close()
9071
yield from server1.wait_closed()
@@ -94,8 +75,7 @@ class Test:
9475
"""Test that asyncio marked functions work in test methods."""
9576

9677
@pytest.mark.asyncio
97-
def test_asyncio_marker_method(self):
78+
def test_asyncio_marker_method(self, event_loop):
9879
"""Test the asyncio pytest marker in a Test class."""
99-
url = 'http://httpbin.org/get'
100-
resp = yield from simple_http_client(url)
101-
assert b'HTTP/1.1 200 OK' in resp
80+
ret = yield from async_coro(event_loop)
81+
assert ret == 'ok'

0 commit comments

Comments
 (0)