Skip to content

Commit 078b30d

Browse files
committed
[refactor] Move automatic marking of async tests in auto mode from pytest_collection_modifyitems to pytest_pycollect_makeitem.
This change causes PytestAsyncioFunction items to exclusively replace those pytest.Function items that are eligible as async tests. That means, once a test item has been substituted, an isinstance check is sufficient to determine if the item is an async test. Signed-off-by: Michael Seifert <[email protected]>
1 parent 4cb7e48 commit 078b30d

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

pytest_asyncio/plugin.py

+6-24
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,13 @@ def pytest_pycollect_makeitem_convert_async_functions_to_subclass(
540540
if isinstance(node, Function):
541541
specialized_item_class = PytestAsyncioFunction.item_subclass_for(node)
542542
if specialized_item_class:
543-
updated_item = specialized_item_class._from_function(node)
543+
if _get_asyncio_mode(
544+
node.config
545+
) == Mode.AUTO and not node.get_closest_marker("asyncio"):
546+
node.add_marker("asyncio")
547+
if node.get_closest_marker("asyncio"):
548+
updated_item = specialized_item_class._from_function(node)
544549
updated_node_collection.append(updated_item)
545-
546550
hook_result.force_result(updated_node_collection)
547551

548552

@@ -649,28 +653,6 @@ def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[No
649653
asyncio.set_event_loop(old_loop)
650654

651655

652-
def pytest_collection_modifyitems(
653-
session: Session, config: Config, items: List[Item]
654-
) -> None:
655-
"""
656-
Marks collected async test items as `asyncio` tests.
657-
658-
The mark is only applied in `AUTO` mode. It is applied to:
659-
660-
- coroutines and async generators
661-
- Hypothesis tests wrapping coroutines
662-
- staticmethods wrapping coroutines
663-
664-
"""
665-
if _get_asyncio_mode(config) != Mode.AUTO:
666-
return
667-
for item in items:
668-
if isinstance(item, PytestAsyncioFunction) and not item.get_closest_marker(
669-
"asyncio"
670-
):
671-
item.add_marker("asyncio")
672-
673-
674656
_REDEFINED_EVENT_LOOP_FIXTURE_WARNING = dedent(
675657
"""\
676658
The event_loop fixture provided by pytest-asyncio has been redefined in

0 commit comments

Comments
 (0)