@@ -887,16 +887,13 @@ def finish(self, request):
887
887
888
888
def execute (self , request ):
889
889
for argname in self ._dependee_fixture_argnames (request ):
890
+ if argname == "request" :
891
+ continue
890
892
fixturedef = request ._get_active_fixturedef (argname )
891
- if argname != "request" :
892
- for fin in fixturedef ._finalizers :
893
- if "request" in getattr (fin , "keywords" , {}):
894
- if self == fin .keywords ["request" ]._fixturedef :
895
- break
896
- else :
897
- fixturedef .addfinalizer (
898
- functools .partial (self .finish , request = request )
899
- )
893
+ if not self ._will_be_finalized_by_fixture (fixturedef ):
894
+ fixturedef .addfinalizer (
895
+ functools .partial (self .finish , request = request )
896
+ )
900
897
901
898
my_cache_key = self .cache_key (request )
902
899
cached_result = getattr (self , "cached_result" , None )
@@ -916,6 +913,25 @@ def execute(self, request):
916
913
hook = self ._fixturemanager .session .gethookproxy (request .node .fspath )
917
914
return hook .pytest_fixture_setup (fixturedef = self , request = request )
918
915
916
+ def _will_be_finalized_by_fixture (self , fixturedef ):
917
+ """Whether or not this fixture be finalized by the passed fixture.
918
+
919
+ Every ``:class:FixtureDef`` keeps a list of all the finishers (tear downs) of
920
+ other ``:class:FixtureDef`` instances that it should run before running its own.
921
+ Finishers are added to this list not by this ``:class:FixtureDef``, but by the
922
+ other ``:class:FixtureDef`` instances. They tell this instance that it's
923
+ responsible for tearing them down before it tears itself down.
924
+
925
+ This method allows a ``:class:FixtureDef`` to check if it has already told
926
+ another ``:class:FixtureDef`` that the latter ``:class:FixtureDef`` is
927
+ responsible for tearing down this ``:class:FixtureDef``.
928
+ """
929
+ for finalizer in fixturedef ._finalizers :
930
+ if "request" in getattr (finalizer , "keywords" , {}):
931
+ if self == finalizer .keywords ["request" ]._fixturedef :
932
+ return True
933
+ return False
934
+
919
935
def _dependee_fixture_argnames (self , request ):
920
936
"""A list of argnames for fixtures that this fixture depends on.
921
937
0 commit comments