Skip to content

Commit 6863959

Browse files
committed
[refactor] The test execution of different pytest.Items are handled in their respective runtest methods, rather than in if clauses inside the pytest_pyfunc_call hook.
Having the item-specific test run code grouped with the item leads to increased code locality, which, in turn makes the code easier to understand. Signed-off-by: Michael Seifert <[email protected]>
1 parent 1a24924 commit 6863959

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

pytest_asyncio/plugin.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ def from_function(cls, function: pytest.Function, /) -> Self:
372372
originalname=function.originalname,
373373
)
374374

375+
def runtest(self) -> None:
376+
if self.get_closest_marker("asyncio"):
377+
self.obj = wrap_in_sync(
378+
self,
379+
self.obj,
380+
)
381+
super().runtest()
382+
375383

376384
class AsyncStaticMethod(pytest.Function):
377385
"""
@@ -394,6 +402,14 @@ def from_function(cls, function: pytest.Function, /) -> Self:
394402
originalname=function.originalname,
395403
)
396404

405+
def runtest(self) -> None:
406+
if self.get_closest_marker("asyncio"):
407+
self.obj = wrap_in_sync(
408+
self,
409+
self.obj,
410+
)
411+
super().runtest()
412+
397413

398414
class AsyncHypothesisTest(pytest.Function):
399415
"""
@@ -416,6 +432,14 @@ def from_function(cls, function: pytest.Function, /) -> Self:
416432
originalname=function.originalname,
417433
)
418434

435+
def runtest(self) -> None:
436+
if self.get_closest_marker("asyncio"):
437+
self.obj.hypothesis.inner_test = wrap_in_sync(
438+
self,
439+
self.obj.hypothesis.inner_test,
440+
)
441+
super().runtest()
442+
419443

420444
_HOLDER: Set[FixtureDef] = set()
421445

@@ -718,16 +742,10 @@ def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> Optional[object]:
718742
"""
719743
marker = pyfuncitem.get_closest_marker("asyncio")
720744
if marker is not None:
721-
if _is_hypothesis_test(pyfuncitem.obj):
722-
pyfuncitem.obj.hypothesis.inner_test = wrap_in_sync(
723-
pyfuncitem,
724-
pyfuncitem.obj.hypothesis.inner_test,
725-
)
726-
elif _is_coroutine_or_asyncgen(pyfuncitem.obj):
727-
pyfuncitem.obj = wrap_in_sync(
728-
pyfuncitem,
729-
pyfuncitem.obj,
730-
)
745+
if isinstance(
746+
pyfuncitem, (AsyncFunction, AsyncHypothesisTest, AsyncStaticMethod)
747+
):
748+
pass
731749
else:
732750
pyfuncitem.warn(
733751
pytest.PytestWarning(

0 commit comments

Comments
 (0)