@@ -887,9 +887,7 @@ def finish(self, request):
887887 self ._finalizers = []
888888
889889 def execute (self , request ):
890- # get required arguments and register our own finish()
891- # with their finalization
892- for argname in self .argnames :
890+ for argname in self ._dependee_fixture_argnames (request ):
893891 fixturedef = request ._get_active_fixturedef (argname )
894892 if argname != "request" :
895893 fixturedef .addfinalizer (functools .partial (self .finish , request = request ))
@@ -912,6 +910,61 @@ def execute(self, request):
912910 hook = self ._fixturemanager .session .gethookproxy (request .node .fspath )
913911 return hook .pytest_fixture_setup (fixturedef = self , request = request )
914912
913+ def _dependee_fixture_argnames (self , request ):
914+ """A list of argnames for fixtures that this fixture depends on.
915+
916+ Given a request, this looks at the currently known list of fixture argnames, and
917+ attempts to determine what slice of the list contains fixtures that it can know
918+ should execute before it. This information is necessary so that this fixture can
919+ know what fixtures to register its finalizer with to make sure that if they
920+ would be torn down, they would tear down this fixture before themselves. It's
921+ crucial for fixtures to be torn down in the inverse order that they were set up
922+ in so that they don't try to clean up something that another fixture is still
923+ depending on.
924+
925+ When autouse fixtures are involved, it can be tricky to figure out when fixtures
926+ should be torn down. To solve this, this method leverages the ``fixturenames``
927+ list provided by the ``request`` object, as this list is at least somewhat
928+ sorted (in terms of the order fixtures are set up in) by the time this method is
929+ reached. It's sorted enough that the starting point of fixtures that depend on
930+ this one can be found using the ``self._parent_request`` stack.
931+
932+ If a request in the ``self._parent_request`` stack has a ``:class:FixtureDef``
933+ associated with it, then that fixture is dependent on this one, so any fixture
934+ names that appear in the list of fixture argnames that come after it can also be
935+ ruled out. The argnames of all fixtures associated with a request in the
936+ ``self._parent_request`` stack are found, and the lowest index argname is
937+ considered the earliest point in the list of fixture argnames where everything
938+ from that point onward can be considered to execute after this fixture.
939+ Everything before this point can be considered fixtures that this fixture
940+ depends on, and so this fixture should register its finalizer with all of them
941+ to ensure that if any of them are to be torn down, they will tear this fixture
942+ down first.
943+
944+ This is the first part of the list of fixture argnames that is returned. The last
945+ part of the list is everything in ``self.argnames`` as those are explicit
946+ dependees of this fixture, so this fixture should definitely register its
947+ finalizer with them.
948+ """
949+ all_fix_names = request .fixturenames
950+ try :
951+ current_fix_index = all_fix_names .index (self .argname )
952+ except ValueError :
953+ current_fix_index = len (request .fixturenames )
954+ parent_fixture_indexes = set ()
955+
956+ parent_request = request ._parent_request
957+ while hasattr (parent_request , "_parent_request" ):
958+ if hasattr (parent_request , "_fixturedef" ):
959+ parent_fix_name = parent_request ._fixturedef .argname
960+ if parent_fix_name in all_fix_names :
961+ parent_fixture_indexes .add (all_fix_names .index (parent_fix_name ))
962+ parent_request = parent_request ._parent_request
963+
964+ stack_slice_index = min ([current_fix_index , * parent_fixture_indexes ])
965+ active_fixture_argnames = all_fix_names [:stack_slice_index ]
966+ return {* active_fixture_argnames , * self .argnames }
967+
915968 def cache_key (self , request ):
916969 return request .param_index if not hasattr (request , "param" ) else request .param
917970
0 commit comments