def _validate_if_using_arg_names(self, argnames, indirect): """ Check if all argnames are being used, by default values, or directly/indirectly. :param List[str] argnames: list of argument names passed to ``parametrize()``. :param indirect: same ``indirect`` parameter of ``parametrize()``. :raise ValueError: if validation fails. """ default_arg_names = set(get_default_arg_names(self.function)) func_name = self.function.__name__ for arg in argnames: if arg not in self.fixturenames: if arg in default_arg_names: fail( "In {}: function already takes an argument '{}' with a default value".format( func_name, arg ), pytrace=False, ) else: if isinstance(indirect, (tuple, list)): name = "fixture" if arg in indirect else "argument" else: name = "fixture" if indirect else "argument" fail( "In {}: function uses no {} '{}'".format(func_name, name, arg), pytrace=False, )
def _validate_if_using_arg_names(self, argnames, indirect): """ Check if all argnames are being used, by default values, or directly/indirectly. :param List[str] argnames: list of argument names passed to ``parametrize()``. :param indirect: same ``indirect`` parameter of ``parametrize()``. :raise ValueError: if validation fails. """ default_arg_names = set(get_default_arg_names(self.function)) for arg in argnames: if arg not in self.fixturenames: if arg in default_arg_names: raise ValueError( "%r already takes an argument %r with a default value" % (self.function, arg) ) else: if isinstance(indirect, (tuple, list)): name = "fixture" if arg in indirect else "argument" else: name = "fixture" if indirect else "argument" raise ValueError("%r uses no %s %r" % (self.function, name, arg))
def _validate_if_using_arg_names(self, argnames, indirect): """ Check if all argnames are being used, by default values, or directly/indirectly. :param List[str] argnames: list of argument names passed to ``parametrize()``. :param indirect: same ``indirect`` parameter of ``parametrize()``. :raise ValueError: if validation fails. """ default_arg_names = set(get_default_arg_names(self.function)) for arg in argnames: if arg not in self.fixturenames: if arg in default_arg_names: raise ValueError( "%r already takes an argument %r with a default value" % (self.function, arg)) else: if isinstance(indirect, (tuple, list)): name = "fixture" if arg in indirect else "argument" else: name = "fixture" if indirect else "argument" raise ValueError("%r uses no %s %r" % (self.function, name, arg))
def parametrize(self, argnames, argvalues, indirect=False, ids=None, scope=None): """ Add new invocations to the underlying test function using the list of argvalues for the given argnames. Parametrization is performed during the collection phase. If you need to setup expensive resources see about setting indirect to do it rather at test setup time. :arg argnames: a comma-separated string denoting one or more argument names, or a list/tuple of argument strings. :arg argvalues: The list of argvalues determines how often a test is invoked with different argument values. If only one argname was specified argvalues is a list of values. If N argnames were specified, argvalues must be a list of N-tuples, where each tuple-element specifies a value for its respective argname. :arg indirect: The list of argnames or boolean. A list of arguments' names (subset of argnames). If True the list contains all names from the argnames. Each argvalue corresponding to an argname in this list will be passed as request.param to its respective argname fixture function so that it can perform more expensive setups during the setup phase of a test rather than at collection time. :arg ids: list of string ids, or a callable. If strings, each is corresponding to the argvalues so that they are part of the test id. If None is given as id of specific test, the automatically generated id for that argument will be used. If callable, it should take one argument (a single argvalue) and return a string or return None. If None, the automatically generated id for that argument will be used. If no ids are provided they will be generated automatically from the argvalues. :arg scope: if specified it denotes the scope of the parameters. The scope is used for grouping tests by parameter instances. It will also override any fixture-function defined scope, allowing to set a dynamic scope using test context or configuration. """ from _pytest.fixtures import scope2index from _pytest.mark import ParameterSet from py.io import saferepr argnames, parameters = ParameterSet._for_parametrize( argnames, argvalues, self.function, self.config ) del argvalues default_arg_names = set(get_default_arg_names(self.function)) if scope is None: scope = _find_parametrized_scope(argnames, self._arg2fixturedefs, indirect) scopenum = scope2index(scope, descr="call to {}".format(self.parametrize)) valtypes = {} for arg in argnames: if arg not in self.fixturenames: if arg in default_arg_names: raise ValueError( "%r already takes an argument %r with a default value" % (self.function, arg) ) else: if isinstance(indirect, (tuple, list)): name = "fixture" if arg in indirect else "argument" else: name = "fixture" if indirect else "argument" raise ValueError("%r uses no %s %r" % (self.function, name, arg)) if indirect is True: valtypes = dict.fromkeys(argnames, "params") elif indirect is False: valtypes = dict.fromkeys(argnames, "funcargs") elif isinstance(indirect, (tuple, list)): valtypes = dict.fromkeys(argnames, "funcargs") for arg in indirect: if arg not in argnames: raise ValueError( "indirect given to %r: fixture %r doesn't exist" % (self.function, arg) ) valtypes[arg] = "params" idfn = None if callable(ids): idfn = ids ids = None if ids: if len(ids) != len(parameters): raise ValueError( "%d tests specified with %d ids" % (len(parameters), len(ids)) ) for id_value in ids: if id_value is not None and not isinstance(id_value, six.string_types): msg = "ids must be list of strings, found: %s (type: %s)" raise ValueError( msg % (saferepr(id_value), type(id_value).__name__) ) ids = idmaker(argnames, parameters, idfn, ids, self.config) newcalls = [] for callspec in self._calls or [CallSpec2(self)]: elements = zip(ids, parameters, count()) for a_id, param, param_index in elements: if len(param.values) != len(argnames): raise ValueError( 'In "parametrize" the number of values ({}) must be ' "equal to the number of names ({})".format( param.values, argnames ) ) newcallspec = callspec.copy() newcallspec.setmulti2( valtypes, argnames, param.values, a_id, param.marks, scopenum, param_index, ) newcalls.append(newcallspec) self._calls = newcalls
def parametrize(self, argnames, argvalues, indirect=False, ids=None, scope=None): """ Add new invocations to the underlying test function using the list of argvalues for the given argnames. Parametrization is performed during the collection phase. If you need to setup expensive resources see about setting indirect to do it rather at test setup time. :arg argnames: a comma-separated string denoting one or more argument names, or a list/tuple of argument strings. :arg argvalues: The list of argvalues determines how often a test is invoked with different argument values. If only one argname was specified argvalues is a list of values. If N argnames were specified, argvalues must be a list of N-tuples, where each tuple-element specifies a value for its respective argname. :arg indirect: The list of argnames or boolean. A list of arguments' names (subset of argnames). If True the list contains all names from the argnames. Each argvalue corresponding to an argname in this list will be passed as request.param to its respective argname fixture function so that it can perform more expensive setups during the setup phase of a test rather than at collection time. :arg ids: list of string ids, or a callable. If strings, each is corresponding to the argvalues so that they are part of the test id. If None is given as id of specific test, the automatically generated id for that argument will be used. If callable, it should take one argument (a single argvalue) and return a string or return None. If None, the automatically generated id for that argument will be used. If no ids are provided they will be generated automatically from the argvalues. :arg scope: if specified it denotes the scope of the parameters. The scope is used for grouping tests by parameter instances. It will also override any fixture-function defined scope, allowing to set a dynamic scope using test context or configuration. """ from _pytest.fixtures import scope2index from _pytest.mark import ParameterSet from py.io import saferepr argnames, parameters = ParameterSet._for_parametrize( argnames, argvalues, self.function, self.config ) del argvalues default_arg_names = set(get_default_arg_names(self.function)) if scope is None: scope = _find_parametrized_scope(argnames, self._arg2fixturedefs, indirect) scopenum = scope2index(scope, descr="call to {}".format(self.parametrize)) valtypes = {} for arg in argnames: if arg not in self.fixturenames: if arg in default_arg_names: raise ValueError( "%r already takes an argument %r with a default value" % (self.function, arg) ) else: if isinstance(indirect, (tuple, list)): name = "fixture" if arg in indirect else "argument" else: name = "fixture" if indirect else "argument" raise ValueError("%r uses no %s %r" % (self.function, name, arg)) if indirect is True: valtypes = dict.fromkeys(argnames, "params") elif indirect is False: valtypes = dict.fromkeys(argnames, "funcargs") elif isinstance(indirect, (tuple, list)): valtypes = dict.fromkeys(argnames, "funcargs") for arg in indirect: if arg not in argnames: raise ValueError( "indirect given to %r: fixture %r doesn't exist" % (self.function, arg) ) valtypes[arg] = "params" idfn = None if callable(ids): idfn = ids ids = None if ids: if len(ids) != len(parameters): raise ValueError( "%d tests specified with %d ids" % (len(parameters), len(ids)) ) for id_value in ids: if id_value is not None and not isinstance(id_value, six.string_types): msg = "ids must be list of strings, found: %s (type: %s)" raise ValueError( msg % (saferepr(id_value), type(id_value).__name__) ) ids = idmaker(argnames, parameters, idfn, ids, self.config) newcalls = [] for callspec in self._calls or [CallSpec2(self)]: elements = zip(ids, parameters, count()) for a_id, param, param_index in elements: if len(param.values) != len(argnames): raise ValueError( 'In "parametrize" the number of values ({}) must be ' "equal to the number of names ({})".format( param.values, argnames ) ) newcallspec = callspec.copy(self) newcallspec.setmulti2( valtypes, argnames, param.values, a_id, param.marks, scopenum, param_index, ) newcalls.append(newcallspec) self._calls = newcalls