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