From e2dbfba3a9ecd13f9f64f36afdb219c1ca2d4732 Mon Sep 17 00:00:00 2001 From: Yuriy Kuzma Date: Sat, 20 Apr 2019 11:42:24 -0500 Subject: [PATCH] Fix issue #112: 'Dynamically calling a fixture causes a runtime error' by failing over to a ThreadPoolExecutor in cases where the expected event loop is already running. Notably this happens when calling 'request.getfixturevalue(argname)' because it dynamically calls a fixture that needs to be setup on an event loop that's already running pytest async tests --- pytest_asyncio/plugin.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 9f4e10d7..98c582fd 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -1,5 +1,6 @@ """pytest-asyncio implementation.""" import asyncio +import concurrent.futures import contextlib import functools import inspect @@ -94,7 +95,10 @@ async def async_finalizer(): request.addfinalizer(finalizer) - return loop.run_until_complete(setup()) + pool = concurrent.futures.ThreadPoolExecutor(1) + loop = asyncio.new_event_loop() + pool.submit(asyncio.set_event_loop, loop).result() + return pool.submit(loop.run_until_complete, setup()).result() fixturedef.func = wrapper