def register_testcases(metafunc, argnames, argvals): """ Add custom parametrization test cases. Based on metafunc's parametrize method. """ from _pytest.python import CallSpec2, _find_parametrized_scope from _pytest.mark import ParameterSet from _pytest.fixtures import scope2index parameter_sets = [ParameterSet(values=val, marks=[], id=None) for val in argvals] metafunc._validate_if_using_arg_names(argnames, False) arg_value_types = metafunc._resolve_arg_value_types(argnames, False) ids = [testcase_id(param_set) for param_set in parameter_sets] scope = _find_parametrized_scope(argnames, metafunc._arg2fixturedefs, False) scopenum = scope2index(scope, descr=f"parametrizex() call in {metafunc.function.__name__}") calls = [] for callspec in metafunc._calls or [CallSpec2(metafunc)]: for param_index, (param_id, param_set) in enumerate(zip(ids, parameter_sets)): newcallspec = callspec.copy() newcallspec.setmulti2( arg_value_types, argnames, param_set.values, param_id, param_set.marks, scopenum, param_index, ) calls.append(newcallspec) metafunc._calls = calls
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 _fixture_ids(self, fixturename): fixturedef = self.fixture_definition(fixturename) metafunc = self.context_item._pyfuncitem.callspec.metafunc # TODO figure out how to avoid using internal things try: argnames, parameters = ParameterSet._for_parametrize( fixturedef.argname, fixturedef.params, metafunc.function, self.config, function_definition=metafunc.definition, ) except TypeError: argnames, parameters = ParameterSet._for_parametrize( fixturedef.argname, fixturedef.params, metafunc.function, self.config, nodeid=self.context_item.nodeid, ) try: ids = metafunc._resolve_arg_ids( argnames, fixturedef.ids, parameters, item=self.context_item, ) except TypeError: ids = metafunc._resolve_arg_ids( argnames, fixturedef.ids, parameters, nodeid=self.context_item.nodeid, ) return ids
def param( *values: _T, marks: Union[MarkDecorator, Collection[Union[MarkDecorator, Mark]]] = (), id: Optional[str] = None, # noqa: A002 # pylint: disable=redefined-builtin idx: Optional[int] = None, key: Optional[Callable[[Tuple[_T, ...]], str]] = None, ) -> ParameterSet: r""" Specify a parameter in `pytest.mark.parametrize <https://docs.pytest.org/en/stable/parametrize.html>`__ calls or :ref:`parametrized fixtures <fixture-parametrize-marks>`. **Examples:** .. code-block:: python @pytest.mark.parametrize("test_input, expected", [ ("3+5", 8), param("6*9", 42, marks=pytest.mark.xfail), param("2**2", 4, idx=0), param("3**2", 9, id="3^2"), param("sqrt(9)", 3, key=itemgetter(0)), ]) def test_eval(test_input, expected): assert eval (test_input) == expected .. versionadded:: 0.4.0 :param \*values: Variable args of the values of the parameter set, in order. :param marks: A single mark or a list of marks to be applied to this parameter set. :param id: The id to attribute to this parameter set. :param idx: The index of the value in ``*values`` to use as the id. :param key: A callable which is given ``values`` (as a :class:`tuple`) and returns the value to use as the id. :rtype: .. clearpage:: """ # noqa: D400 if len([x for x in (id, idx, key) if x is not None]) > 1: raise ValueError("'id', 'idx' and 'key' are mutually exclusive.") if idx is not None: # pytest will catch the type error later on id = cast(str, values[idx]) # noqa: A001 # pylint: disable=redefined-builtin elif key is not None: id = key(values) # noqa: A001 # pylint: disable=redefined-builtin return ParameterSet.param(*values, marks=marks, id=id)
def test_parameterset_extractfrom(argval, expected): extracted = ParameterSet.extract_from(argval) assert extracted == expected
def assert_test_is_not_selected(keyword): reprec = testdir.inline_run("-k", keyword, p) passed, skipped, failed = reprec.countoutcomes() dlist = reprec.getcalls("pytest_deselected") assert passed + skipped + failed == 0 deselected_tests = dlist[0].items assert len(deselected_tests) == 1 assert_test_is_not_selected("__") assert_test_is_not_selected("()") @pytest.mark.parametrize('argval, expected', [ (pytest.mark.skip()( (1, 2)), ParameterSet(values=(1, 2), marks=[pytest.mark.skip], id=None)), (pytest.mark.xfail(pytest.mark.skip()((1, 2))), ParameterSet( values=(1, 2), marks=[pytest.mark.xfail, pytest.mark.skip], id=None)), ]) @pytest.mark.filterwarnings('ignore') def test_parameterset_extractfrom(argval, expected): extracted = ParameterSet.extract_from(argval) assert extracted == expected def test_legacy_transfer(): class FakeModule(object): pytestmark = [] class FakeClass(object):
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_parameterize( argnames, argvalues, self.function) del argvalues 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, 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 ({0}) must be ' 'equal to the number of names ({1})'.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
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 ParameterSet argnames, parameters = ParameterSet._for_parametrize( argnames, argvalues, self.function, self.config, function_definition=self.definition, ) del argvalues if scope is None: scope = _find_parametrized_scope(argnames, self._arg2fixturedefs, indirect) self._validate_if_using_arg_names(argnames, indirect) arg_values_types = self._resolve_arg_value_types(argnames, indirect) ids = self._resolve_arg_ids(argnames, ids, parameters, item=self.definition) scopenum = scope2index( scope, descr="parametrize() call in {}".format(self.function.__name__) ) # create the new calls: if we are parametrize() multiple times (by applying the decorator # more than once) then we accumulate those calls generating the cartesian product # of all calls newcalls = [] for callspec in self._calls or [CallSpec2(self)]: for param_index, (param_id, param_set) in enumerate(zip(ids, parameters)): newcallspec = callspec.copy() newcallspec.setmulti2( arg_values_types, argnames, param_set.values, param_id, param_set.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 argnames, parameters = ParameterSet._for_parametrize( argnames, argvalues, self.function, self.config ) del argvalues if scope is None: scope = _find_parametrized_scope(argnames, self._arg2fixturedefs, indirect) self._validate_if_using_arg_names(argnames, indirect) arg_values_types = self._resolve_arg_value_types(argnames, indirect) ids = self._resolve_arg_ids(argnames, ids, parameters) scopenum = scope2index(scope, descr="call to {}".format(self.parametrize)) # create the new calls: if we are parametrize() multiple times (by applying the decorator # more than once) then we accumulate those calls generating the cartesian product # of all calls newcalls = [] for callspec in self._calls or [CallSpec2(self)]: for param_index, (param_id, param_set) in enumerate(zip(ids, parameters)): newcallspec = callspec.copy() newcallspec.setmulti2( arg_values_types, argnames, param_set.values, param_id, param_set.marks, scopenum, param_index, ) newcalls.append(newcallspec) self._calls = newcalls