예제 #1
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 isclass(obj):
        if collector.istestclass(obj, name):
            Class = collector._getcustomclass("Class")
            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))):
            collector.warn(
                code="C2",
                message="cannot collect %r because it is not a function." % name,
            )
        elif getattr(obj, "__test__", True):
            if is_generator(obj):
                res = Generator(name, parent=collector)
            else:
                res = list(collector._genfunctions(name, obj))
            outcome.force_result(res)
예제 #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)
예제 #3
0
파일: python.py 프로젝트: EdgarChen/servo
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 isclass(obj):
        if collector.istestclass(obj, name):
            Class = collector._getcustomclass("Class")
            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))):
            collector.warn(code="C2", message="cannot collect %r because it is not a function."
                           % name, )
        elif getattr(obj, "__test__", True):
            if is_generator(obj):
                res = Generator(name, parent=collector)
            else:
                res = list(collector._genfunctions(name, obj))
            outcome.force_result(res)
예제 #4
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)
예제 #5
0
def _idval(val, argname, idx, idfn, config=None):
    if idfn:
        s = None
        try:
            s = idfn(val)
        except Exception:
            # See issue https://github.com/pytest-dev/pytest/issues/2169
            import warnings

            msg = "Raised while trying to determine id of parameter %s at position %d." % (
                argname, idx)
            msg += "\nUpdate your code as this will raise an error in pytest-4.0."
            warnings.warn(msg, DeprecationWarning)
        if s:
            return ascii_escaped(s)

    if config:
        hook_id = config.hook.pytest_make_parametrize_id(config=config,
                                                         val=val,
                                                         argname=argname)
        if hook_id:
            return hook_id

    if isinstance(val, STRING_TYPES):
        return ascii_escaped(val)
    elif isinstance(val, (float, int, bool, NoneType)):
        return str(val)
    elif isinstance(val, REGEX_TYPE):
        return ascii_escaped(val.pattern)
    elif enum is not None and isinstance(val, enum.Enum):
        return str(val)
    elif (isclass(val) or isfunction(val)) and hasattr(val, "__name__"):
        return val.__name__
    return str(argname) + str(idx)
예제 #6
0
파일: python.py 프로젝트: Coder206/servo
def _idval(val, argname, idx, idfn, config=None):
    if idfn:
        s = None
        try:
            s = idfn(val)
        except Exception:
            # See issue https://github.com/pytest-dev/pytest/issues/2169
            import warnings

            msg = "Raised while trying to determine id of parameter %s at position %d." % (
                argname, idx
            )
            msg += "\nUpdate your code as this will raise an error in pytest-4.0."
            warnings.warn(msg, DeprecationWarning)
        if s:
            return ascii_escaped(s)

    if config:
        hook_id = config.hook.pytest_make_parametrize_id(
            config=config, val=val, argname=argname
        )
        if hook_id:
            return hook_id

    if isinstance(val, STRING_TYPES):
        return ascii_escaped(val)
    elif isinstance(val, (float, int, bool, NoneType)):
        return str(val)
    elif isinstance(val, REGEX_TYPE):
        return ascii_escaped(val.pattern)
    elif enum is not None and isinstance(val, enum.Enum):
        return str(val)
    elif (isclass(val) or isfunction(val)) and hasattr(val, "__name__"):
        return val.__name__
    return str(argname) + str(idx)
예제 #7
0
def _idval(val, argname, idx, idfn, item, config):
    if idfn:
        s = None
        try:
            s = idfn(val)
        except Exception as e:
            # See issue https://github.com/pytest-dev/pytest/issues/2169
            msg = "{}: error raised while trying to determine id of parameter '{}' at position {}\n"
            msg = msg.format(item.nodeid, argname, idx)
            # we only append the exception type and message because on Python 2 reraise does nothing
            msg += "  {}: {}\n".format(type(e).__name__, e)
            six.raise_from(ValueError(msg), e)
        if s:
            return ascii_escaped(s)

    if config:
        hook_id = config.hook.pytest_make_parametrize_id(
            config=config, val=val, argname=argname
        )
        if hook_id:
            return hook_id

    if isinstance(val, STRING_TYPES):
        return ascii_escaped(val)
    elif isinstance(val, (float, int, bool, NoneType)):
        return str(val)
    elif isinstance(val, REGEX_TYPE):
        return ascii_escaped(val.pattern)
    elif enum is not None and isinstance(val, enum.Enum):
        return str(val)
    elif (isclass(val) or isfunction(val)) and hasattr(val, "__name__"):
        return val.__name__
    return str(argname) + str(idx)
예제 #8
0
def _idval(val, argname, idx, idfn, item, config):
    if idfn:
        s = None
        try:
            s = idfn(val)
        except Exception as e:
            # See issue https://github.com/pytest-dev/pytest/issues/2169
            msg = "{}: error raised while trying to determine id of parameter '{}' at position {}\n"
            msg = msg.format(item.nodeid, argname, idx)
            # we only append the exception type and message because on Python 2 reraise does nothing
            msg += "  {}: {}\n".format(type(e).__name__, e)
            six.raise_from(ValueError(msg), e)
        if s:
            return ascii_escaped(s)

    if config:
        hook_id = config.hook.pytest_make_parametrize_id(
            config=config, val=val, argname=argname
        )
        if hook_id:
            return hook_id

    if isinstance(val, STRING_TYPES):
        return ascii_escaped(val)
    elif isinstance(val, (float, int, bool, NoneType)):
        return str(val)
    elif isinstance(val, REGEX_TYPE):
        return ascii_escaped(val.pattern)
    elif enum is not None and isinstance(val, enum.Enum):
        return str(val)
    elif (isclass(val) or isfunction(val)) and hasattr(val, "__name__"):
        return val.__name__
    return str(argname) + str(idx)