def test_safe_isclass(): assert safe_isclass(type) is True class CrappyClass(Exception): @property def __class__(self): assert False, "Should be ignored" assert safe_isclass(CrappyClass()) is False
def test_safe_isclass(): assert safe_isclass(type) is True class CrappyClass(Exception): # Type ignored because it's bypassed intentionally. @property # type: ignore def __class__(self): assert False, "Should be ignored" assert safe_isclass(CrappyClass()) is False
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)
def pytest_pycollect_makeitem(collector, name, obj): """ @see PyCollector._genfunctions @see _pytest.python """ if safe_isclass(obj): if collector.istestclass(obj, name) or (issubclass(obj, AbstractTestCase) and obj != AbstractTestCase): if hasattr(pytest.Class, "from_parent"): return pytest.Class.from_parent(collector, name=name) else: return pytest.Class(name, parent=collector) else: obj = getattr(obj, "__func__", obj) if (inspect.isfunction(obj) or inspect.isfunction(get_real_func(obj))) and getattr(obj, "__test__", True) and isinstance(collector, pytest.Instance) and hasattr(obj, "pytestmark"): if not is_generator(obj): return list(collector._genfunctions(name, obj)) else: return [] else: return []