|
| 1 | +"""Quick'n'dirty unit tests using async and await syntax.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import sys |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +pytestmark = pytest.mark.skipif( |
| 9 | + sys.version_info[:2] < (3, 5), |
| 10 | + reason='This syntax is only valid in 3.5 and newer') |
| 11 | + |
| 12 | + |
| 13 | +@asyncio.coroutine |
| 14 | +def async_coro(loop): |
| 15 | + yield from asyncio.sleep(0, loop=loop) |
| 16 | + return 'ok' |
| 17 | + |
| 18 | + |
| 19 | +# PEP 492 added the syntax for these tests to Python 3.5. Older versions |
| 20 | +# can't even parse the module to let skipif exclude the tests. To get |
| 21 | +# around this, all tests are defined in a string which can be compiled |
| 22 | +# and executed on appropriate versions of Python. |
| 23 | +_possible_tests = ''' |
| 24 | +@pytest.mark.asyncio |
| 25 | +async def test_asyncio_marker(): |
| 26 | + """Test the asyncio pytest marker.""" |
| 27 | +
|
| 28 | +
|
| 29 | +@pytest.mark.asyncio |
| 30 | +async def test_asyncio_marker_with_default_param(a_param=None): |
| 31 | + """Test the asyncio pytest marker.""" |
| 32 | +
|
| 33 | +
|
| 34 | +@pytest.mark.asyncio_process_pool |
| 35 | +async def test_asyncio_process_pool_marker(event_loop): |
| 36 | + ret = await async_coro(event_loop) |
| 37 | + assert ret == 'ok' |
| 38 | +
|
| 39 | +
|
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_unused_port_fixture(unused_tcp_port, event_loop): |
| 42 | + """Test the unused TCP port fixture.""" |
| 43 | + async def closer(_, writer): |
| 44 | + writer.close() |
| 45 | +
|
| 46 | + server1 = await asyncio.start_server(closer, host='localhost', |
| 47 | + port=unused_tcp_port, |
| 48 | + loop=event_loop) |
| 49 | +
|
| 50 | + server1.close() |
| 51 | + await server1.wait_closed() |
| 52 | +
|
| 53 | +
|
| 54 | +@pytest.mark.asyncio |
| 55 | +async def test_unused_port_factory_fixture(unused_tcp_port_factory, event_loop): |
| 56 | + """Test the unused TCP port factory fixture.""" |
| 57 | +
|
| 58 | + async def closer(_, writer): |
| 59 | + writer.close() |
| 60 | +
|
| 61 | + port1, port2, port3 = (unused_tcp_port_factory(), unused_tcp_port_factory(), |
| 62 | + unused_tcp_port_factory()) |
| 63 | +
|
| 64 | + server1 = await asyncio.start_server(closer, host='localhost', |
| 65 | + port=port1, |
| 66 | + loop=event_loop) |
| 67 | + server2 = await asyncio.start_server(closer, host='localhost', |
| 68 | + port=port2, |
| 69 | + loop=event_loop) |
| 70 | + server3 = await asyncio.start_server(closer, host='localhost', |
| 71 | + port=port3, |
| 72 | + loop=event_loop) |
| 73 | +
|
| 74 | + for port in port1, port2, port3: |
| 75 | + with pytest.raises(IOError): |
| 76 | + await asyncio.start_server(closer, host='localhost', |
| 77 | + port=port, |
| 78 | + loop=event_loop) |
| 79 | +
|
| 80 | + server1.close() |
| 81 | + await server1.wait_closed() |
| 82 | + server2.close() |
| 83 | + await server2.wait_closed() |
| 84 | + server3.close() |
| 85 | + await server3.wait_closed() |
| 86 | +
|
| 87 | +
|
| 88 | +class Test: |
| 89 | + """Test that asyncio marked functions work in test methods.""" |
| 90 | +
|
| 91 | + @pytest.mark.asyncio |
| 92 | + async def test_asyncio_marker_method(self, event_loop): |
| 93 | + """Test the asyncio pytest marker in a Test class.""" |
| 94 | + ret = await async_coro(event_loop) |
| 95 | + assert ret == 'ok' |
| 96 | +''' |
| 97 | + |
| 98 | +if sys.version_info[:2] >= (3, 5): |
| 99 | + exec(compile(_possible_tests, __file__, 'exec')) |
0 commit comments