Exemple #1
0
    def assume(expr, msg=''):
        """
        Checks the expression, if it's false, add it to the
        list of failed assumptions. Also, add the locals at each failed
        assumption, if showlocals is set.

        :param expr: Expression to 'assert' on.
        :param msg: Message to display if the assertion fails.
        :return: None
        """
        if not expr:
            (frame, filename, line, funcname,
             contextlist) = inspect.stack()[1][0:5]
            # get filename, line, and context
            filename = os.path.relpath(filename)
            context = contextlist[0].lstrip() if not msg else msg
            # format entry
            entry = "{filename}:{line}: AssumptionFailure\n\t{context}".format(
                **locals())
            # add entry
            pytest._failed_assumptions.append(entry)
            if pytest.config.option.showlocals:
                # Debatable whether we should display locals for
                # every failed assertion, or just the final one.
                # I'm defaulting to per-assumption, just because the vars
                # can easily change between assumptions.
                pretty_locals = [
                    "%-10s = %s" % (name, saferepr(val))
                    for name, val in frame.f_locals.items()
                ]
                pytest._assumption_locals.append(pretty_locals)
Exemple #2
0
    def _resolve_arg_ids(self, argnames, ids, parameters):
        """Resolves the actual ids for the given argnames, based on the ``ids`` parameter given
        to ``parametrize``.

        :param List[str] argnames: list of argument names passed to ``parametrize()``.
        :param ids: the ids parameter of the parametrized call (see docs).
        :param List[ParameterSet] parameters: the list of parameter values, same size as ``argnames``.
        :rtype: List[str]
        :return: the list of ids for each argname given
        """
        from py.io import saferepr

        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)
        return ids
Exemple #3
0
    def _resolve_arg_ids(self, argnames, ids, parameters, item):
        """Resolves the actual ids for the given argnames, based on the ``ids`` parameter given
        to ``parametrize``.

        :param List[str] argnames: list of argument names passed to ``parametrize()``.
        :param ids: the ids parameter of the parametrized call (see docs).
        :param List[ParameterSet] parameters: the list of parameter values, same size as ``argnames``.
        :param Item item: the item that generated this parametrized call.
        :rtype: List[str]
        :return: the list of ids for each argname given
        """
        from py.io import saferepr

        idfn = None
        if callable(ids):
            idfn = ids
            ids = None
        if ids:
            func_name = self.function.__name__
            if len(ids) != len(parameters):
                msg = "In {}: {} parameter sets specified, with different number of ids: {}"
                fail(msg.format(func_name, len(parameters), len(ids)), pytrace=False)
            for id_value in ids:
                if id_value is not None and not isinstance(id_value, six.string_types):
                    msg = "In {}: ids must be list of strings, found: {} (type: {!r})"
                    fail(
                        msg.format(func_name, saferepr(id_value), type(id_value)),
                        pytrace=False,
                    )
        ids = idmaker(argnames, parameters, idfn, ids, self.config, item=item)
        return ids
Exemple #4
0
    def assume(expr, msg=''):
        """
        Checks the expression, if it's false, add it to the
        list of failed assumptions. Also, add the locals at each failed
        assumption, if showlocals is set.

        :param expr: Expression to 'assert' on.
        :param msg: Message to display if the assertion fails.
        :return: None
        """
        if not expr:
            (frame, filename, line, funcname, contextlist) = inspect.stack()[1][0:5]
            # get filename, line, and context
            filename = os.path.relpath(filename)
            context = contextlist[0].lstrip() if not msg else msg
            # format entry
            entry = "{filename}:{line}: AssumptionFailure\n\t{context}".format(**locals())
            # add entry
            pytest._failed_assumptions.append(entry)
            if pytest.config.option.showlocals:
                # Debatable whether we should display locals for
                # every failed assertion, or just the final one.
                # I'm defaulting to per-assumption, just because the vars
                # can easily change between assumptions.
                pretty_locals = ["%-10s = %s" % (name, saferepr(val))
                                 for name, val in frame.f_locals.items()]
                pytest._assumption_locals.append(pretty_locals)
Exemple #5
0
    def _resolve_arg_ids(self, argnames, ids, parameters):
        """Resolves the actual ids for the given argnames, based on the ``ids`` parameter given
        to ``parametrize``.

        :param List[str] argnames: list of argument names passed to ``parametrize()``.
        :param ids: the ids parameter of the parametrized call (see docs).
        :param List[ParameterSet] parameters: the list of parameter values, same size as ``argnames``.
        :rtype: List[str]
        :return: the list of ids for each argname given
        """
        from py.io import saferepr

        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)
        return ids
Exemple #6
0
    def __exit__(self, exc_type, exc_val, exc_tb):
        __tracebackhide__ = True
        pretty_locals = None
        entry = None
        tb = None
        stack_level = 2 if self._enter_from_call else 1
        (frame, filename, line, funcname,
         contextlist) = inspect.stack()[stack_level][0:5]
        # get filename, line, and context
        try:
            filename = os.path.relpath(filename)
        except ValueError:
            pass  # filename is on a different mount than the current dir (Windows)

        context = "" if contextlist is None else contextlist[0].lstrip()

        if exc_type is None:
            # format entry
            entry = u"{filename}:{line}: AssumptionSuccess\n>>\t{context}".format(
                **locals())
            pytest._hook_assume_pass(lineno=line, entry=entry)

            self._last_status = True
            return True

        elif issubclass(exc_type, AssertionError):
            if exc_val:
                context += "{}: {}\n\n".format(exc_type.__name__, exc_val)

            # format entry
            entry = u"{filename}:{line}: AssumptionFailure\n>>\t{context}".format(
                **locals())

            # Debatable whether we should display locals for
            # every failed assertion, or just the final one.
            # I'm defaulting to per-assumption, just because vars
            # can easily change between assumptions.
            pretty_locals = [
                "\t%-10s = %s" % (name, saferepr(val))
                for name, val in frame.f_locals.items()
            ]

            pytest._hook_assume_fail(lineno=line, entry=entry)
            _FAILED_ASSUMPTIONS.append(Assumption(entry, exc_tb,
                                                  pretty_locals))

            self._last_status = False
            return True

        else:
            # Another type of exception, let it rise uncaught
            return
Exemple #7
0
    def check(expr, msg='', hard=False, issue=None):
        """
        Checks the expression, if it's false, add it to the
        list of failed assumptions. Also, add the locals at each failed
        assumption, if showlocals is set.

        :param expr: Expression to 'assert' on.
        :param msg: Message to display
        :param hard: if test should continue if the assertion fails
        :param issue: known issue, log WAIVE
        :return: None
        """
        if CHECKLOGGER is None:
            set_logger()
        # get filename, line, and context
        (frame, filename, line, funcname, contextlist) = \
            inspect.stack()[1][0:5]
        filename = os.path.relpath(filename)
        context = contextlist[0].lstrip() if not msg else msg
        if not expr:
            # format entry
            entry = "{filename}:{line}: "\
                    "AssumptionFailure\n\t{context}".format(**locals())
            # add entry
            if pytest.config.option.showlocals:
                # Debatable whether we should display locals for
                # every failed assertion, or just the final one.
                # I'm defaulting to per-assumption, just because the vars
                # can easily change between assumptions.
                pretty_locals = [
                    "%-10s = %s" % (name, saferepr(val))
                    for name, val in frame.f_locals.items()
                ]
                pytest._assumption_locals.append(pretty_locals)
            if issue is not None:
                entry = '{entry}\n\tKnown issue: {issue}'.format(**locals())
                CHECKLOGGER.waived(entry)
                CHECKLOGGER.add_issue(issue)
            else:
                CHECKLOGGER.failed(entry)
            if hard:
                # fail the test
                pytest.fail(entry)
        else:
            # format entry
            entry = "{filename}:{line}: "\
                    "AssumptionPassed\n\t{context}".format(**locals())
            # track passed ones too
            # add entry
            CHECKLOGGER.passed(entry)
Exemple #8
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
Exemple #9
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
Exemple #10
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