From 5301d8bac10bd9b1b8b4bf7d79a6e5756a8bf4dc Mon Sep 17 00:00:00 2001 From: sqwishy Date: Sat, 27 Aug 2016 13:52:37 -0700 Subject: [PATCH 1/2] Support timeout kwargs for async mark to run the test with a timeout. --- pytest_asyncio/plugin.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index c6d2d7e4..bac9565d 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -58,9 +58,15 @@ def pytest_pyfunc_call(pyfuncitem): funcargs = pyfuncitem.funcargs testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames} + test_coroutine = pyfuncitem.obj(**testargs) + + if 'timeout' in pyfuncitem.keywords[marker_name].kwargs: + timeout = pyfuncitem.keywords[marker_name].kwargs['timeout'] + test_coroutine = asyncio.wait_for(test_coroutine, timeout=timeout) + try: event_loop.run_until_complete( - asyncio.async(pyfuncitem.obj(**testargs), loop=event_loop)) + asyncio.async(test_coroutine, loop=event_loop)) return True finally: if forbid_global_loop: From d7f5bed8fcc36b1b38320f14b94218773502fe1d Mon Sep 17 00:00:00 2001 From: sqwishy Date: Sat, 27 Aug 2016 14:02:26 -0700 Subject: [PATCH 2/2] Adds a test for the timeout option. --- tests/test_simple.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_simple.py b/tests/test_simple.py index 1b305904..ee20fa27 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -58,6 +58,12 @@ def test_asyncio_process_pool_marker(event_loop): assert ret == 'ok' +@pytest.mark.xfail(raises=asyncio.TimeoutError) +@pytest.mark.asyncio(timeout=0.05) +def test_asyncio_marker_fail(): + yield from asyncio.sleep(0.1) + + @pytest.mark.asyncio def test_unused_port_fixture(unused_tcp_port, event_loop): """Test the unused TCP port fixture."""