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
Example #2
0
    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
Example #3
0
    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
Example #4
0
    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
Example #5
0
    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
Example #6
0
    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