Example #1
0
def test_parameterset_for_fail_at_collect(testdir):
    testdir.makeini(
        """
    [pytest]
    {}=fail_at_collect
    """.format(
            EMPTY_PARAMETERSET_OPTION
        )
    )

    config = testdir.parseconfig()
    from _pytest.mark import pytest_configure, get_empty_parameterset_mark
    from _pytest.compat import getfslineno

    pytest_configure(config)

    test_func = all
    func_name = test_func.__name__
    _, func_lineno = getfslineno(test_func)
    expected_errmsg = r"Empty parameter set in '%s' at line %d" % (
        func_name,
        func_lineno,
    )

    with pytest.raises(Collector.CollectError, match=expected_errmsg):
        get_empty_parameterset_mark(config, ["a"], test_func)
Example #2
0
def pytest_pycollect_makeitem(collector, name, obj):
    outcome = yield
    res = outcome.get_result()
    if res is not None:
        return
    # nothing was collected elsewhere, let's do it here
    if safe_isclass(obj):
        if collector.istestclass(obj, name):
            outcome.force_result(Class(name, parent=collector))
    elif collector.istestfunction(obj, name):
        # mock seems to store unbound methods (issue473), normalize it
        obj = getattr(obj, "__func__", obj)
        # We need to try and unwrap the function if it's a functools.partial
        # or a funtools.wrapped.
        # We musn't if it's been wrapped with mock.patch (python 2 only)
        if not (isfunction(obj) or isfunction(get_real_func(obj))):
            filename, lineno = getfslineno(obj)
            warnings.warn_explicit(
                message=PytestWarning(
                    "cannot collect %r because it is not a function." % name
                ),
                category=None,
                filename=str(filename),
                lineno=lineno + 1,
            )
        elif getattr(obj, "__test__", True):
            if is_generator(obj):
                res = Function(name, parent=collector)
                reason = deprecated.YIELD_TESTS.format(name=name)
                res.add_marker(MARK_GEN.xfail(run=False, reason=reason))
                res.warn(PytestWarning(reason))
            else:
                res = list(collector._genfunctions(name, obj))
            outcome.force_result(res)
Example #3
0
def pytest_pycollect_makeitem(collector, name, obj):
    outcome = yield
    res = outcome.get_result()
    if res is not None:
        return
    # nothing was collected elsewhere, let's do it here
    if safe_isclass(obj):
        if collector.istestclass(obj, name):
            outcome.force_result(Class(name, parent=collector))
    elif collector.istestfunction(obj, name):
        # mock seems to store unbound methods (issue473), normalize it
        obj = getattr(obj, "__func__", obj)
        # We need to try and unwrap the function if it's a functools.partial
        # or a funtools.wrapped.
        # We musn't if it's been wrapped with mock.patch (python 2 only)
        if not (isfunction(obj) or isfunction(get_real_func(obj))):
            filename, lineno = getfslineno(obj)
            warnings.warn_explicit(
                message=PytestWarning(
                    "cannot collect %r because it is not a function." % name
                ),
                category=None,
                filename=str(filename),
                lineno=lineno + 1,
            )
        elif getattr(obj, "__test__", True):
            if is_generator(obj):
                res = Function(name, parent=collector)
                reason = deprecated.YIELD_TESTS.format(name=name)
                res.add_marker(MARK_GEN.xfail(run=False, reason=reason))
                res.warn(PytestWarning(reason))
            else:
                res = list(collector._genfunctions(name, obj))
            outcome.force_result(res)
Example #4
0
 def _factorytraceback(self):
     lines = []
     for fixturedef in self._get_fixturestack():
         factory = fixturedef.func
         fs, lineno = getfslineno(factory)
         p = self._pyfuncitem.session.fspath.bestrelpath(fs)
         args = _format_args(factory)
         lines.append("%s:%d:  def %s%s" % (p, lineno + 1, factory.__name__, args))
     return lines
Example #5
0
def get_line_number(item):
    location = getattr(item, 'location', None)
    if location is not None:
        return location[1]
    obj = getattr(item, 'obj', None)
    if obj is not None:
        try:
            from _pytest.compat import getfslineno
            return getfslineno(obj)[1]
        except:
            pass
    return None
Example #6
0
 def reportinfo(self):
     # XXX caching?
     obj = self.obj
     compat_co_firstlineno = getattr(obj, "compat_co_firstlineno", None)
     if isinstance(compat_co_firstlineno, int):
         # nose compatibility
         fspath = sys.modules[obj.__module__].__file__
         if fspath.endswith(".pyc"):
             fspath = fspath[:-1]
         lineno = compat_co_firstlineno
     else:
         fspath, lineno = getfslineno(obj)
     modpath = self.getmodpath()
     assert isinstance(lineno, int)
     return fspath, lineno, modpath
Example #7
0
 def reportinfo(self):
     # XXX caching?
     obj = self.obj
     compat_co_firstlineno = getattr(obj, 'compat_co_firstlineno', None)
     if isinstance(compat_co_firstlineno, int):
         # nose compatibility
         fspath = sys.modules[obj.__module__].__file__
         if fspath.endswith(".pyc"):
             fspath = fspath[:-1]
         lineno = compat_co_firstlineno
     else:
         fspath, lineno = getfslineno(obj)
     modpath = self.getmodpath()
     assert isinstance(lineno, int)
     return fspath, lineno, modpath
Example #8
0
def get_fslocation_from_item(item):
    """Tries to extract the actual location from an item, depending on available attributes:

    * "fslocation": a pair (path, lineno)
    * "obj": a Python object that the item wraps.
    * "fspath": just a path

    :rtype: a tuple of (str|LocalPath, int) with filename and line number.
    """
    result = getattr(item, "location", None)
    if result is not None:
        return result[:2]
    obj = getattr(item, "obj", None)
    if obj is not None:
        return getfslineno(obj)
    return getattr(item, "fspath", "unknown location"), -1
Example #9
0
def get_fslocation_from_item(item):
    """Tries to extract the actual location from an item, depending on available attributes:

    * "fslocation": a pair (path, lineno)
    * "obj": a Python object that the item wraps.
    * "fspath": just a path

    :rtype: a tuple of (str|LocalPath, int) with filename and line number.
    """
    result = getattr(item, "location", None)
    if result is not None:
        return result[:2]
    obj = getattr(item, "obj", None)
    if obj is not None:
        return getfslineno(obj)
    return getattr(item, "fspath", "unknown location"), -1
Example #10
0
    def formatrepr(self):
        tblines = []
        addline = tblines.append
        stack = [self.request._pyfuncitem.obj]
        stack.extend(map(lambda x: x.func, self.fixturestack))
        msg = self.msg
        if msg is not None:
            # the last fixture raise an error, let's present
            # it at the requesting side
            stack = stack[:-1]
        for function in stack:
            fspath, lineno = getfslineno(function)
            try:
                lines, _ = inspect.getsourcelines(get_real_func(function))
            except (IOError, IndexError, TypeError):
                error_msg = "file %s, line %s: source code not available"
                addline(error_msg % (fspath, lineno + 1))
            else:
                addline("file %s, line %s" % (fspath, lineno + 1))
                for i, line in enumerate(lines):
                    line = line.rstrip()
                    addline("  " + line)
                    if line.lstrip().startswith("def"):
                        break

        if msg is None:
            fm = self.request._fixturemanager
            available = set()
            parentid = self.request._pyfuncitem.parent.nodeid
            for name, fixturedefs in fm._arg2fixturedefs.items():
                faclist = list(fm._matchfactories(fixturedefs, parentid))
                if faclist:
                    available.add(name)
            if self.argname in available:
                msg = " recursive dependency involving fixture '{}' detected".format(
                    self.argname)
            else:
                msg = "fixture '{}' not found".format(self.argname)
            msg += "\n available fixtures: {}".format(", ".join(
                sorted(available)))
            msg += "\n use 'pytest --fixtures [testpath]' for help on them."

        return FixtureLookupErrorRepr(fspath, lineno, tblines, msg,
                                      self.argname)
Example #11
0
    def formatrepr(self):
        tblines = []
        addline = tblines.append
        stack = [self.request._pyfuncitem.obj]
        stack.extend(map(lambda x: x.func, self.fixturestack))
        msg = self.msg
        if msg is not None:
            # the last fixture raise an error, let's present
            # it at the requesting side
            stack = stack[:-1]
        for function in stack:
            fspath, lineno = getfslineno(function)
            try:
                lines, _ = inspect.getsourcelines(get_real_func(function))
            except (IOError, IndexError, TypeError):
                error_msg = "file %s, line %s: source code not available"
                addline(error_msg % (fspath, lineno + 1))
            else:
                addline("file %s, line %s" % (fspath, lineno + 1))
                for i, line in enumerate(lines):
                    line = line.rstrip()
                    addline("  " + line)
                    if line.lstrip().startswith("def"):
                        break

        if msg is None:
            fm = self.request._fixturemanager
            available = set()
            parentid = self.request._pyfuncitem.parent.nodeid
            for name, fixturedefs in fm._arg2fixturedefs.items():
                faclist = list(fm._matchfactories(fixturedefs, parentid))
                if faclist:
                    available.add(name)
            if self.argname in available:
                msg = " recursive dependency involving fixture '{}' detected".format(
                    self.argname
                )
            else:
                msg = "fixture '{}' not found".format(self.argname)
            msg += "\n available fixtures: {}".format(", ".join(sorted(available)))
            msg += "\n use 'pytest --fixtures [testpath]' for help on them."

        return FixtureLookupErrorRepr(fspath, lineno, tblines, msg, self.argname)
Example #12
0
def report_process_crash(item, result):
    try:
        from _pytest.compat import getfslineno
    except ImportError:
        # pytest<4.2
        path, lineno = item._getfslineno()
    else:
        path, lineno = getfslineno(item)
    info = ("%s:%s: running the test CRASHED with signal %d" %
            (path, lineno, result.signal))
    from _pytest import runner
    # pytest >= 4.1
    has_from_call = getattr(runner.CallInfo, "from_call", None) is not None
    if has_from_call:
        call = runner.CallInfo.from_call(lambda: 0 / 0, "???")
    else:
        call = runner.CallInfo(lambda: 0 / 0, "???")
    call.excinfo = info
    rep = runner.pytest_runtest_makereport(item, call)
    if result.out:
        rep.sections.append(("captured stdout", result.out))
    if result.err:
        rep.sections.append(("captured stderr", result.err))
    return rep
Example #13
0
 def _getfslineno(self):
     return getfslineno(self.obj)
Example #14
0
 def _getfslineno(self):
     return getfslineno(self.obj)
Example #15
0
def fail_fixturefunc(fixturefunc, msg):
    fs, lineno = getfslineno(fixturefunc)
    location = "%s:%s" % (fs, lineno+1)
    source = _pytest._code.Source(fixturefunc)
    pytest.fail(msg + ":\n\n" + str(source.indent()) + "\n" + location,
                pytrace=False)
Example #16
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 #17
0
def fail_fixturefunc(fixturefunc, msg):
    fs, lineno = getfslineno(fixturefunc)
    location = "%s:%s" % (fs, lineno + 1)
    source = _pytest._code.Source(fixturefunc)
    fail(msg + ":\n\n" + str(source.indent()) + "\n" + location, pytrace=False)
Example #18
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