def test_parameterset_extractfrom(argval, expected): from _pytest.deprecated import MARK_PARAMETERSET_UNPACKING warn_called = [] class DummyItem: def warn(self, warning): warn_called.append(warning) extracted = ParameterSet.extract_from(argval, belonging_definition=DummyItem()) assert extracted == expected assert warn_called == [MARK_PARAMETERSET_UNPACKING]
def test_parameterset_extractfrom(argval, expected): extracted = ParameterSet.extract_from(argval) assert extracted == expected
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 MARK_GEN, ParameterSet from py.io import saferepr if not isinstance(argnames, (tuple, list)): argnames = [x.strip() for x in argnames.split(",") if x.strip()] force_tuple = len(argnames) == 1 else: force_tuple = False parameters = [ ParameterSet.extract_from(x, legacy_force_tuple=force_tuple) for x in argvalues] del argvalues if not parameters: fs, lineno = getfslineno(self.function) reason = "got empty parameter set %r, function %s at %s:%d" % ( argnames, self.function.__name__, fs, lineno) mark = MARK_GEN.skip(reason=reason) parameters.append(ParameterSet( values=(NOTSET,) * len(argnames), marks=[mark], id=None, )) if scope is None: scope = _find_parametrized_scope(argnames, self._arg2fixturedefs, indirect) scopenum = scope2index(scope, descr='call to {0}'.format(self.parametrize)) valtypes = {} for arg in argnames: if arg not in self.fixturenames: 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, py.builtin._basestring): 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 ({0}) must be ' 'equal to the number of names ({1})'.format( param.values, argnames)) newcallspec = callspec.copy(self) newcallspec.setmulti(valtypes, argnames, param.values, a_id, param.deprecated_arg_dict, 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 MARK_GEN, ParameterSet from py.io import saferepr if not isinstance(argnames, (tuple, list)): argnames = [x.strip() for x in argnames.split(",") if x.strip()] force_tuple = len(argnames) == 1 else: force_tuple = False parameters = [ ParameterSet.extract_from(x, legacy_force_tuple=force_tuple) for x in argvalues ] del argvalues if not parameters: fs, lineno = getfslineno(self.function) reason = "got empty parameter set %r, function %s at %s:%d" % ( argnames, self.function.__name__, fs, lineno) mark = MARK_GEN.skip(reason=reason) parameters.append( ParameterSet( values=(NOTSET, ) * len(argnames), marks=[mark], id=None, )) if scope is None: scope = _find_parametrized_scope(argnames, self._arg2fixturedefs, indirect) scopenum = scope2index(scope, descr='call to {0}'.format(self.parametrize)) valtypes = {} for arg in argnames: if arg not in self.fixturenames: 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, py.builtin._basestring): 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 ({0}) must be ' 'equal to the number of names ({1})'.format( param.values, argnames)) newcallspec = callspec.copy(self) newcallspec.setmulti(valtypes, argnames, param.values, a_id, param.deprecated_arg_dict, scopenum, param_index) newcalls.append(newcallspec) self._calls = newcalls