7373from _pytest .scope import _ScopeName
7474from _pytest .scope import HIGH_SCOPES
7575from _pytest .scope import Scope
76+ from _pytest .warning_types import PytestRemovedIn9Warning
7677
7778
7879if sys .version_info < (3 , 11 ):
@@ -575,6 +576,7 @@ def _get_active_fixturedef(
575576 # The are no fixtures with this name applicable for the function.
576577 if not fixturedefs :
577578 raise FixtureLookupError (argname , self )
579+
578580 # A fixture may override another fixture with the same name, e.g. a
579581 # fixture in a module can override a fixture in a conftest, a fixture in
580582 # a class can override a fixture in the module, and so on.
@@ -593,6 +595,32 @@ def _get_active_fixturedef(
593595 raise FixtureLookupError (argname , self )
594596 fixturedef = fixturedefs [index ]
595597
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+
596624 # Prepare a SubRequest object for calling the fixture.
597625 try :
598626 callspec = self ._pyfuncitem .callspec
@@ -805,7 +833,7 @@ def formatrepr(self) -> FixtureLookupErrorRepr:
805833 stack = [self .request ._pyfuncitem .obj ]
806834 stack .extend (map (lambda x : x .func , self .fixturestack ))
807835 msg = self .msg
808- if msg is not None :
836+ if msg is not None and len ( stack ) > 1 :
809837 # The last fixture raise an error, let's present
810838 # it at the requesting side.
811839 stack = stack [:- 1 ]
@@ -959,6 +987,8 @@ def __init__(
959987 ids : tuple [object | None , ...] | Callable [[Any ], object | None ] | None = None ,
960988 * ,
961989 _ispytest : bool = False ,
990+ # only used to emit a deprecationwarning, can be removed in pytest9
991+ _autouse : bool = False ,
962992 ) -> None :
963993 check_ispytest (_ispytest )
964994 # The "base" node ID for the fixture.
@@ -1005,6 +1035,9 @@ def __init__(
10051035 self .cached_result : _FixtureCachedResult [FixtureValue ] | None = None
10061036 self ._finalizers : Final [list [Callable [[], object ]]] = []
10071037
1038+ # only used to emit a deprecationwarning, can be removed in pytest9
1039+ self ._autouse = _autouse
1040+
10081041 @property
10091042 def scope (self ) -> _ScopeName :
10101043 """Scope string, one of "function", "class", "module", "package", "session"."""
@@ -1666,6 +1699,7 @@ def _register_fixture(
16661699 params = params ,
16671700 ids = ids ,
16681701 _ispytest = True ,
1702+ _autouse = autouse ,
16691703 )
16701704
16711705 faclist = self ._arg2fixturedefs .setdefault (name , [])
0 commit comments