73
73
from _pytest .scope import _ScopeName
74
74
from _pytest .scope import HIGH_SCOPES
75
75
from _pytest .scope import Scope
76
+ from _pytest .warning_types import PytestRemovedIn9Warning
76
77
77
78
78
79
if sys .version_info < (3 , 11 ):
@@ -575,6 +576,7 @@ def _get_active_fixturedef(
575
576
# The are no fixtures with this name applicable for the function.
576
577
if not fixturedefs :
577
578
raise FixtureLookupError (argname , self )
579
+
578
580
# A fixture may override another fixture with the same name, e.g. a
579
581
# fixture in a module can override a fixture in a conftest, a fixture in
580
582
# a class can override a fixture in the module, and so on.
@@ -593,6 +595,32 @@ def _get_active_fixturedef(
593
595
raise FixtureLookupError (argname , self )
594
596
fixturedef = fixturedefs [index ]
595
597
598
+ if not inspect .iscoroutinefunction (self .function ) and (
599
+ inspect .iscoroutinefunction (fixturedef .func )
600
+ or inspect .isasyncgenfunction (fixturedef .func )
601
+ ):
602
+ if fixturedef ._autouse :
603
+ warnings .warn (
604
+ PytestRemovedIn9Warning (
605
+ "Sync test requested an async fixture with autouse=True. "
606
+ "If you intended to use the fixture you may want to make the "
607
+ "test asynchronous. If you did not intend to use it you should "
608
+ "restructure your test setup. "
609
+ "This will turn into an error in pytest 9."
610
+ ),
611
+ stacklevel = 3 ,
612
+ )
613
+ else :
614
+ raise FixtureLookupError (
615
+ argname ,
616
+ self ,
617
+ (
618
+ "ERROR: Sync test requested async fixture. "
619
+ "You may want to make the test asynchronous and run it with "
620
+ "a suitable async framework test plugin."
621
+ ),
622
+ )
623
+
596
624
# Prepare a SubRequest object for calling the fixture.
597
625
try :
598
626
callspec = self ._pyfuncitem .callspec
@@ -805,7 +833,7 @@ def formatrepr(self) -> FixtureLookupErrorRepr:
805
833
stack = [self .request ._pyfuncitem .obj ]
806
834
stack .extend (map (lambda x : x .func , self .fixturestack ))
807
835
msg = self .msg
808
- if msg is not None :
836
+ if msg is not None and len ( stack ) > 1 :
809
837
# The last fixture raise an error, let's present
810
838
# it at the requesting side.
811
839
stack = stack [:- 1 ]
@@ -959,6 +987,8 @@ def __init__(
959
987
ids : tuple [object | None , ...] | Callable [[Any ], object | None ] | None = None ,
960
988
* ,
961
989
_ispytest : bool = False ,
990
+ # only used to emit a deprecationwarning, can be removed in pytest9
991
+ _autouse : bool = False ,
962
992
) -> None :
963
993
check_ispytest (_ispytest )
964
994
# The "base" node ID for the fixture.
@@ -1005,6 +1035,9 @@ def __init__(
1005
1035
self .cached_result : _FixtureCachedResult [FixtureValue ] | None = None
1006
1036
self ._finalizers : Final [list [Callable [[], object ]]] = []
1007
1037
1038
+ # only used to emit a deprecationwarning, can be removed in pytest9
1039
+ self ._autouse = _autouse
1040
+
1008
1041
@property
1009
1042
def scope (self ) -> _ScopeName :
1010
1043
"""Scope string, one of "function", "class", "module", "package", "session"."""
@@ -1666,6 +1699,7 @@ def _register_fixture(
1666
1699
params = params ,
1667
1700
ids = ids ,
1668
1701
_ispytest = True ,
1702
+ _autouse = autouse ,
1669
1703
)
1670
1704
1671
1705
faclist = self ._arg2fixturedefs .setdefault (name , [])
0 commit comments