@@ -911,30 +911,33 @@ def _cleanup_calls_list(metafunc, fix_closure_tree, calls, nodes, pending):
911911 calls [i ] = c_with_dummy [0 ]
912912 c = calls [i ]
913913
914- # B/ some non-parametrized fixtures may also need to be explicitly deactivated in some callspecs
915- # otherwise they will be setup/teardown.
914+ # B/ function-scoped non-parametrized fixtures also need to be explicitly deactivated in the callspecs
915+ # where they are not required, otherwise they will be setup/teardown.
916916 #
917917 # For this we use a dirty hack: we add a parameter with they name in the callspec, it seems to be propagated
918918 # in the `request`. TODO is there a better way?
919- # for fixture in list(fix_closure_tree):
920- # for fixture_name, fixdef in metafunc._arg2fixturedefs.items():
919+ f_scope = get_pytest_function_scopenum ()
921920 for fixture_name in fix_closure_tree .get_not_always_used ():
922921 try :
923922 fixdef = metafunc ._arg2fixturedefs [fixture_name ] # noqa
924923 except KeyError :
925924 continue # dont raise any error here and let pytest say "not found"
926925
927- if fixture_name not in c .params and fixture_name not in c .funcargs :
928- if not n .requires (fixture_name ):
929- # explicitly add it as discarded by creating a parameter value for it.
930- c .params [fixture_name ] = NOT_USED
931- c .indices [fixture_name ] = 1
932- c ._arg2scopenum [fixture_name ] = get_pytest_scopenum (fixdef [- 1 ].scope ) # noqa
933- else :
934- # explicitly add it as active
935- c .params [fixture_name ] = 'used'
936- c .indices [fixture_name ] = 0
937- c ._arg2scopenum [fixture_name ] = get_pytest_scopenum (fixdef [- 1 ].scope ) # noqa
926+ this_scopenum = fixdef [- 1 ].scopenum
927+
928+ # only do this for function-scoped fixtures, module or session scoped fixtures should remain active
929+ if this_scopenum == f_scope :
930+ if fixture_name not in c .params and fixture_name not in c .funcargs :
931+ if not n .requires (fixture_name ):
932+ # explicitly add it as discarded by creating a parameter value for it.
933+ c .params [fixture_name ] = NOT_USED
934+ c .indices [fixture_name ] = 1
935+ c ._arg2scopenum [fixture_name ] = this_scopenum # get_pytest_scopenum(fixdef[-1].scope) # noqa
936+ else :
937+ # explicitly add it as active
938+ c .params [fixture_name ] = 'used'
939+ c .indices [fixture_name ] = 0
940+ c ._arg2scopenum [fixture_name ] = this_scopenum # get_pytest_scopenum(fixdef[-1].scope) # noqa
938941
939942
940943# def get_calls_for_partition(metafunc, super_closure, p_idx, pending):
0 commit comments