Skip to content

Commit d837555

Browse files
committed
[refactor] Introduced new item type "AsyncHypothesisTest" which represents a coroutine or async generator to which a @hypothesis.given decorator was applied.
The decorator returns a synchronous function and therefore needs special treatment by pytest-asyncio. Signed-off-by: Michael Seifert <[email protected]>
1 parent b1d0ce7 commit d837555

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

pytest_asyncio/plugin.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,28 @@ def from_function(cls, function: pytest.Function, /) -> Self:
373373
)
374374

375375

376+
class AsyncHypothesisTest(pytest.Function):
377+
"""
378+
Pytest item that is coroutine or an asynchronous generator decorated by
379+
@hypothesis.given.
380+
"""
381+
382+
@classmethod
383+
def from_function(cls, function: pytest.Function, /) -> Self:
384+
"""
385+
Instantiates an AsyncFunction from the specified pytest.Function item.
386+
"""
387+
return cls.from_parent(
388+
function.parent,
389+
name=function.name,
390+
callspec=getattr(function, "callspec", None),
391+
callobj=function.obj,
392+
fixtureinfo=function._fixtureinfo,
393+
keywords=function.keywords,
394+
originalname=function.originalname,
395+
)
396+
397+
376398
_HOLDER: Set[FixtureDef] = set()
377399

378400

@@ -414,11 +436,14 @@ def pytest_pycollect_makeitem_convert_async_functions_to_subclass(
414436
node_iterator = iter((node_or_list_of_nodes,))
415437
updated_node_collection = []
416438
for node in node_iterator:
417-
if isinstance(node, pytest.Function) and _is_coroutine_or_asyncgen(obj):
418-
async_function = AsyncFunction.from_function(node)
419-
updated_node_collection.append(async_function)
420-
else:
421-
updated_node_collection.append(node)
439+
updated_item = node
440+
if isinstance(node, pytest.Function):
441+
if _is_coroutine_or_asyncgen(obj):
442+
updated_item = AsyncFunction.from_function(node)
443+
if _is_hypothesis_test(obj) and _hypothesis_test_wraps_coroutine(obj):
444+
updated_item = AsyncHypothesisTest.from_function(node)
445+
updated_node_collection.append(updated_item)
446+
422447
hook_result.force_result(updated_node_collection)
423448

424449

0 commit comments

Comments
 (0)