def process_env_mark(self, metafunc): if hasattr(metafunc.function, self.NAME): args = getattr(metafunc.function, self.NAME).args kwargs = getattr(metafunc.function, self.NAME).kwargs.copy() scope = kwargs.pop('scope', 'function') indirect = kwargs.pop('indirect', False) filter_unused = kwargs.pop('filter_unused', True) selector = kwargs.pop('selector', ALL) gen_func = kwargs.pop('gen_func', providers_by_class) # If parametrize doesn't get you what you need, steal this and modify as needed kwargs.update({'selector': selector}) argnames, argvalues, idlist = gen_func(metafunc, *args, **kwargs) # Filter out argnames that aren't requested on the metafunc test item, so not all tests # need all fixtures to run, and tests not using gen_func's fixtures aren't parametrized. if filter_unused: argnames, argvalues = fixture_filter(metafunc, argnames, argvalues) # See if we have to parametrize at all after filtering parametrize(metafunc, argnames, argvalues, indirect=indirect, ids=idlist, scope=scope, selector=selector)
def process_env_mark(self, metafunc): if hasattr(metafunc.function, self.NAME): mark = None for mark in getattr(metafunc.function, self.NAME): if 'override' in mark.kwargs.keys() and mark.kwargs['override']: break else: if len(getattr(metafunc.function, self.NAME)._marks) >= 2: raise Exception( "You have an override provider without " "specifying the override flag [{}]".format(metafunc.function.__name__) ) args = mark.args kwargs = mark.kwargs.copy() if 'override' in kwargs: kwargs.pop('override') scope = kwargs.pop('scope', 'function') indirect = kwargs.pop('indirect', False) filter_unused = kwargs.pop('filter_unused', True) selector = kwargs.pop('selector', ALL) gen_func = kwargs.pop('gen_func', providers_by_class) # If parametrize doesn't get you what you need, steal this and modify as needed kwargs.update({'selector': selector}) argnames, argvalues, idlist = gen_func(metafunc, *args, **kwargs) # Filter out argnames that aren't requested on the metafunc test item, so not all tests # need all fixtures to run, and tests not using gen_func's fixtures aren't parametrized. if filter_unused: argnames, argvalues = fixture_filter(metafunc, argnames, argvalues) # See if we have to parametrize at all after filtering parametrize( metafunc, argnames, argvalues, indirect=indirect, ids=idlist, scope=scope, selector=selector )
def pytest_generate_tests(metafunc): # Pass through of args and kwargs argnames, argvalues, idlist = gen_func(metafunc, *args, **kwargs) # Filter out argnames that aren't requested on the metafunc test item, so not all tests # need all fixtures to run, and tests not using gen_func's fixtures aren't parametrized. if filter_unused: argnames, argvalues = fixture_filter(metafunc, argnames, argvalues) # See if we have to parametrize at all after filtering parametrize(metafunc, argnames, argvalues, indirect=indirect, ids=idlist, scope=scope)
def process_env_mark(self, metafunc): """ Process the provider env marks Notes: provider markers can be applied at multiple layers (module, class, function) provider markers automatically override at lower layers (function overrides all) provider markers can supply their own fixture_name, to support multiple providers Args: metafunc: pytest metafunc object Returns: Parametrizes metafunc object directly, returns nothing """ # organize by fixture_name kwarg to the marker # iter_markers returns most local mark first, maybe don't need override marks_by_fixture = self.get_closest_kwarg_markers(metafunc.definition) if marks_by_fixture is None: return # process each mark, defaulting fixture_name for fixture_name, mark in marks_by_fixture.items(): # mark is either the lowest marker (automatic override), or has custom fixture_name logger.debug(f'Parametrizing provider env mark {mark}') args = mark.args kwargs = mark.kwargs.copy() if kwargs.pop('override', False): logger.warning( 'provider marker included override kwarg, this is unnecessary' ) scope = kwargs.pop('scope', 'function') indirect = kwargs.pop('indirect', False) filter_unused = kwargs.pop('filter_unused', True) selector = kwargs.pop('selector', ONE_PER_VERSION) gen_func = kwargs.pop('gen_func', providers_by_class) # If parametrize doesn't get you what you need, steal this and modify as needed kwargs.update({'selector': selector}) argnames, argvalues, idlist = gen_func(metafunc, *args, **kwargs) # Filter out argnames that aren't requested on the metafunc test item, # so not all tests need all fixtures to run, and tests not using gen_func's # fixtures aren't parametrized. if filter_unused: argnames, argvalues = fixture_filter(metafunc, argnames, argvalues) # See if we have to parametrize at all after filtering parametrize(metafunc, argnames, argvalues, indirect=indirect, ids=idlist, scope=scope, selector=selector)
def process_env_mark(self, metafunc): if hasattr(metafunc.function, self.NAME): mark_dict = defaultdict(list) for mark in getattr(metafunc.function, self.NAME): # Find all provider-ish markers fixture_name = mark.kwargs.get('fixture_name', 'provider') mark_dict[fixture_name].append(mark) for name, marks in mark_dict.items(): mark = None for mark in marks: if mark.kwargs.get('override', False): break else: if len(mark_dict[name]) >= 2: raise Exception( "You have an override provider without " "specifying the override flag [{}]".format( metafunc.function.__name__)) args = mark.args kwargs = mark.kwargs.copy() if 'override' in kwargs: kwargs.pop('override') scope = kwargs.pop('scope', 'function') indirect = kwargs.pop('indirect', False) filter_unused = kwargs.pop('filter_unused', True) selector = kwargs.pop('selector', ONE_PER_VERSION) gen_func = kwargs.pop('gen_func', providers_by_class) # If parametrize doesn't get you what you need, steal this and modify as needed kwargs.update({'selector': selector}) argnames, argvalues, idlist = gen_func(metafunc, *args, **kwargs) # Filter out argnames that aren't requested on the metafunc test item, # so not all tests need all fixtures to run, and tests not using gen_func's # fixtures aren't parametrized. if filter_unused: argnames, argvalues = fixture_filter( metafunc, argnames, argvalues) # See if we have to parametrize at all after filtering parametrize(metafunc, argnames, argvalues, indirect=indirect, ids=idlist, scope=scope, selector=selector)
def process_env_mark(self, metafunc): if hasattr(metafunc.function, self.NAME): args = getattr(metafunc.function, self.NAME).args kwargs = getattr(metafunc.function, self.NAME).kwargs.copy() scope = kwargs.pop('scope', 'function') indirect = kwargs.pop('indirect', False) filter_unused = kwargs.pop('filter_unused', True) selector = kwargs.pop('selector', ALL) gen_func = kwargs.pop('gen_func', providers_by_class) # If parametrize doesn't get you what you need, steal this and modify as needed kwargs.update({'selector': selector}) argnames, argvalues, idlist = gen_func(metafunc, *args, **kwargs) # Filter out argnames that aren't requested on the metafunc test item, so not all tests # need all fixtures to run, and tests not using gen_func's fixtures aren't parametrized. if filter_unused: argnames, argvalues = fixture_filter(metafunc, argnames, argvalues) # See if we have to parametrize at all after filtering parametrize( metafunc, argnames, argvalues, indirect=indirect, ids=idlist, scope=scope, selector=selector )
def test_fixture_filter(structure): argnames, argvalues = fixture_filter(FakeMetaFunc(), ['a', 'b'], [structure]) argvalues = [_values(x) for x in argvalues] assert argvalues == [(1, )]
def test_fixture_filter(structure): argnames, argvalues = fixture_filter(FakeMetaFunc(), ['a', 'b'], [structure]) argvalues = [_values(x) for x in argvalues] assert argvalues == [(1,)]