Skip to content

Commit 82cebfe

Browse files
seifertmTinche
authored andcommitted
Removed a layer of indirection when checking for markers.
There is only one marker and one fixture provided by pytest-asyncio. Retrieving these from a dictionary seems unnecessary complicated.
1 parent 383de19 commit 82cebfe

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

pytest_asyncio/plugin.py

+19-31
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,15 @@ async def setup():
124124

125125
if fixturedef.argname == "event_loop" and 'asyncio' in request.keywords:
126126
loop = outcome.get_result()
127-
for kw in _markers_2_fixtures.keys():
128-
if kw not in request.keywords:
129-
continue
130-
policy = asyncio.get_event_loop_policy()
131-
try:
132-
old_loop = policy.get_event_loop()
133-
except RuntimeError as exc:
134-
if 'no current event loop' not in str(exc):
135-
raise
136-
old_loop = None
137-
policy.set_event_loop(loop)
138-
fixturedef.addfinalizer(lambda: policy.set_event_loop(old_loop))
127+
policy = asyncio.get_event_loop_policy()
128+
try:
129+
old_loop = policy.get_event_loop()
130+
except RuntimeError as exc:
131+
if 'no current event loop' not in str(exc):
132+
raise
133+
old_loop = None
134+
policy.set_event_loop(loop)
135+
fixturedef.addfinalizer(lambda: policy.set_event_loop(old_loop))
139136

140137

141138
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
@@ -144,14 +141,13 @@ def pytest_pyfunc_call(pyfuncitem):
144141
Run asyncio marked test functions in an event loop instead of a normal
145142
function call.
146143
"""
147-
for marker_name, fixture_name in _markers_2_fixtures.items():
148-
if marker_name in pyfuncitem.keywords:
149-
if getattr(pyfuncitem.obj, 'is_hypothesis_test', False):
150-
pyfuncitem.obj.hypothesis.inner_test = wrap_in_sync(
151-
pyfuncitem.obj.hypothesis.inner_test
152-
)
153-
else:
154-
pyfuncitem.obj = wrap_in_sync(pyfuncitem.obj)
144+
if 'asyncio' in pyfuncitem.keywords:
145+
if getattr(pyfuncitem.obj, 'is_hypothesis_test', False):
146+
pyfuncitem.obj.hypothesis.inner_test = wrap_in_sync(
147+
pyfuncitem.obj.hypothesis.inner_test
148+
)
149+
else:
150+
pyfuncitem.obj = wrap_in_sync(pyfuncitem.obj)
155151
yield
156152

157153

@@ -170,10 +166,9 @@ def inner(**kwargs):
170166

171167

172168
def pytest_runtest_setup(item):
173-
for marker, fixture in _markers_2_fixtures.items():
174-
if marker in item.keywords and fixture not in item.fixturenames:
175-
# inject an event loop fixture for all async tests
176-
item.fixturenames.append(fixture)
169+
if 'asyncio' in item.keywords and 'event_loop' not in item.fixturenames:
170+
# inject an event loop fixture for all async tests
171+
item.fixturenames.append('event_loop')
177172
if item.get_closest_marker("asyncio") is not None \
178173
and not getattr(item.obj, 'hypothesis', False) \
179174
and getattr(item.obj, 'is_hypothesis_test', False):
@@ -183,13 +178,6 @@ def pytest_runtest_setup(item):
183178
)
184179

185180

186-
# maps marker to the name of the event loop fixture that will be available
187-
# to marked test functions
188-
_markers_2_fixtures = {
189-
'asyncio': 'event_loop',
190-
}
191-
192-
193181
@pytest.yield_fixture
194182
def event_loop(request):
195183
"""Create an instance of the default event loop for each test case."""

0 commit comments

Comments
 (0)