@@ -353,19 +353,14 @@ async def setup():
353
353
fixturedef .func = _async_fixture_wrapper
354
354
355
355
356
- class AsyncFunction (pytest .Function ):
357
- """Pytest item that is a coroutine or an asynchronous generator"""
358
-
359
- @staticmethod
360
- def can_substitute (item : pytest .Function ) -> bool :
361
- """Returns whether the specified function can be replaced by this class"""
362
- func = item .obj
363
- return _is_coroutine_or_asyncgen (func )
356
+ class PytestAsyncioFunction (pytest .Function ):
357
+ """Base class for all test functions managed by pytest-asyncio."""
364
358
365
359
@classmethod
366
360
def from_function (cls , function : pytest .Function , / ) -> Self :
367
361
"""
368
- Instantiates an AsyncFunction from the specified pytest.Function item.
362
+ Instantiates this specific PytestAsyncioFunction type from the specified
363
+ pytest.Function item.
369
364
"""
370
365
return cls .from_parent (
371
366
function .parent ,
@@ -377,6 +372,20 @@ def from_function(cls, function: pytest.Function, /) -> Self:
377
372
originalname = function .originalname ,
378
373
)
379
374
375
+ @staticmethod
376
+ def can_substitute (item : pytest .Function ) -> bool :
377
+ """Returns whether the specified function can be replaced by this class"""
378
+ raise NotImplementedError ()
379
+
380
+
381
+ class AsyncFunction (PytestAsyncioFunction ):
382
+ """Pytest item that is a coroutine or an asynchronous generator"""
383
+
384
+ @staticmethod
385
+ def can_substitute (item : pytest .Function ) -> bool :
386
+ func = item .obj
387
+ return _is_coroutine_or_asyncgen (func )
388
+
380
389
def runtest (self ) -> None :
381
390
if self .get_closest_marker ("asyncio" ):
382
391
self .obj = wrap_in_sync (
@@ -386,35 +395,19 @@ def runtest(self) -> None:
386
395
super ().runtest ()
387
396
388
397
389
- class AsyncStaticMethod (pytest . Function ):
398
+ class AsyncStaticMethod (PytestAsyncioFunction ):
390
399
"""
391
400
Pytest item that is a coroutine or an asynchronous generator
392
401
decorated with staticmethod
393
402
"""
394
403
395
404
@staticmethod
396
405
def can_substitute (item : pytest .Function ) -> bool :
397
- """Returns whether the specified function can be replaced by this class"""
398
406
func = item .obj
399
407
return isinstance (func , staticmethod ) and _is_coroutine_or_asyncgen (
400
408
func .__func__
401
409
)
402
410
403
- @classmethod
404
- def from_function (cls , function : pytest .Function , / ) -> Self :
405
- """
406
- Instantiates an AsyncStaticMethod from the specified pytest.Function item.
407
- """
408
- return cls .from_parent (
409
- function .parent ,
410
- name = function .name ,
411
- callspec = getattr (function , "callspec" , None ),
412
- callobj = function .obj ,
413
- fixtureinfo = function ._fixtureinfo ,
414
- keywords = function .keywords ,
415
- originalname = function .originalname ,
416
- )
417
-
418
411
def runtest (self ) -> None :
419
412
if self .get_closest_marker ("asyncio" ):
420
413
self .obj = wrap_in_sync (
@@ -424,33 +417,17 @@ def runtest(self) -> None:
424
417
super ().runtest ()
425
418
426
419
427
- class AsyncHypothesisTest (pytest . Function ):
420
+ class AsyncHypothesisTest (PytestAsyncioFunction ):
428
421
"""
429
422
Pytest item that is coroutine or an asynchronous generator decorated by
430
423
@hypothesis.given.
431
424
"""
432
425
433
426
@staticmethod
434
427
def can_substitute (item : pytest .Function ) -> bool :
435
- """Returns whether the specified function can be replaced by this class"""
436
428
func = item .obj
437
429
return _is_hypothesis_test (func ) and _hypothesis_test_wraps_coroutine (func )
438
430
439
- @classmethod
440
- def from_function (cls , function : pytest .Function , / ) -> Self :
441
- """
442
- Instantiates an AsyncFunction from the specified pytest.Function item.
443
- """
444
- return cls .from_parent (
445
- function .parent ,
446
- name = function .name ,
447
- callspec = getattr (function , "callspec" , None ),
448
- callobj = function .obj ,
449
- fixtureinfo = function ._fixtureinfo ,
450
- keywords = function .keywords ,
451
- originalname = function .originalname ,
452
- )
453
-
454
431
def runtest (self ) -> None :
455
432
if self .get_closest_marker ("asyncio" ):
456
433
self .obj .hypothesis .inner_test = wrap_in_sync (
@@ -589,7 +566,7 @@ def pytest_collection_modifyitems(
589
566
if _get_asyncio_mode (config ) != Mode .AUTO :
590
567
return
591
568
for item in items :
592
- if isinstance (item , ( AsyncFunction , AsyncHypothesisTest , AsyncStaticMethod ) ):
569
+ if isinstance (item , PytestAsyncioFunction ):
593
570
item .add_marker ("asyncio" )
594
571
595
572
@@ -750,9 +727,7 @@ def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> Optional[object]:
750
727
"""
751
728
marker = pyfuncitem .get_closest_marker ("asyncio" )
752
729
if marker is not None :
753
- if isinstance (
754
- pyfuncitem , (AsyncFunction , AsyncHypothesisTest , AsyncStaticMethod )
755
- ):
730
+ if isinstance (pyfuncitem , PytestAsyncioFunction ):
756
731
pass
757
732
else :
758
733
pyfuncitem .warn (
0 commit comments