python类Class()的实例源码

classes.py 文件源码 项目:pytest-relaxed 作者: bitprophet 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def collect(self):
        # Get whatever our parent picked up as valid test items (given our
        # relaxed name constraints above). It'll be nearly all module contents.
        items = super(SpecModule, self).collect()
        collected = []
        for item in items:
            # Replace Class objects with recursive SpecInstances (via
            # SpecClasses, so we don't lose some bits of parent Class).
            # TODO: this may be way more than is necessary; possible we can
            # recurse & unpack here, as long as we preserve parent/child
            # relationships correctly?
            # NOTE: we could explicitly skip unittest objects here (we'd want
            # them to be handled by pytest's own unittest support) but since
            # those are almost always in test_prefixed_filenames anyways...meh
            if isinstance(item, Class):
                item = SpecClass(item.name, item.parent)
            # TODO: pytest.mark objects (i.e. @pytest.mark.something applied to
            # an entire testcase class) may need special treatment here
            # Collect regardless of whether we replaced the item
            collected.append(item)
        return collected


# NOTE: no need to inherit from RelaxedMixin here as it doesn't do much by
# its lonesome
collect.py 文件源码 项目:GSM-scanner 作者: yosriayed 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def test_collector_attributes(testdir):
    testdir.makeconftest("""
        import pytest
        def pytest_pycollect_makeitem(collector):
            assert collector.Function == pytest.Function
            assert collector.Class == pytest.Class
            assert collector.Instance == pytest.Instance
            assert collector.Module == pytest.Module
    """)
    testdir.makepyfile("""
         def test_hello():
            pass
    """)
    result = testdir.runpytest()
    result.stdout.fnmatch_lines([
        "*1 passed*",
    ])
collect.py 文件源码 项目:GSM-scanner 作者: yosriayed 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def test_customize_through_attributes(testdir):
    testdir.makeconftest("""
        import pytest
        class MyFunction(pytest.Function):
            pass
        class MyInstance(pytest.Instance):
            Function = MyFunction
        class MyClass(pytest.Class):
            Instance = MyInstance

        def pytest_pycollect_makeitem(collector, name, obj):
            if name.startswith("MyTestClass"):
                return MyClass(name, parent=collector)
    """)
    testdir.makepyfile("""
         class MyTestClass:
            def test_hello(self):
                pass
    """)
    result = testdir.runpytest("--collect-only")
    result.stdout.fnmatch_lines([
        "*MyClass*",
        "*MyInstance*",
        "*MyFunction*test_hello*",
    ])
test_collection.py 文件源码 项目:GSM-scanner 作者: yosriayed 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_getparent(self, testdir):
        modcol = testdir.getmodulecol("""
            class TestClass:
                 def test_foo():
                     pass
        """)
        cls = testdir.collect_by_name(modcol, "TestClass")
        fn = testdir.collect_by_name(
            testdir.collect_by_name(cls, "()"), "test_foo")

        parent = fn.getparent(pytest.Module)
        assert parent is modcol

        parent = fn.getparent(pytest.Function)
        assert parent is fn

        parent = fn.getparent(pytest.Class)
        assert parent is cls
pytestplugin.py 文件源码 项目:Flask_Blog 作者: sugarguo 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:Flask_Blog 作者: sugarguo 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
fixtures.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def pytest_namespace():
    scopename2class.update({
        'class': pytest.Class,
        'module': pytest.Module,
        'function': pytest.Item,
    })
    return {
        'fixture': fixture,
        'yield_fixture': yield_fixture,
        'collect': {'_fillfuncargs': fillfixtures}
    }
fixtures.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def cls(self):
        """ class (can be None) where the test function was collected. """
        clscol = self._pyfuncitem.getparent(pytest.Class)
        if clscol:
            return clscol.obj
pytestplugin.py 文件源码 项目:QXSConsolas 作者: qxsch 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:flasky 作者: RoseOu 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):

    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            name.startswith("test_") and \
            isinstance(collector, pytest.Instance):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:oa_qian 作者: sunqb 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def pytest_collection_modifyitems(session, config, items):
    # look for all those classes that specify __backend__ and
    # expand them out into per-database test cases.

    # this is much easier to do within pytest_pycollect_makeitem, however
    # pytest is iterating through cls.__dict__ as makeitem is
    # called which causes a "dictionary changed size" error on py3k.
    # I'd submit a pullreq for them to turn it into a list first, but
    # it's to suit the rather odd use case here which is that we are adding
    # new classes to a module on the fly.

    rebuilt_items = collections.defaultdict(list)
    test_classes = set(item.parent for item in items)
    for test_class in test_classes:
        for sub_cls in plugin_base.generate_sub_tests(
                test_class.cls, test_class.parent.module):
            if sub_cls is not test_class.cls:
                list_ = rebuilt_items[test_class.cls]

                for inst in pytest.Class(
                        sub_cls.__name__,
                        parent=test_class.parent.parent).collect():
                    list_.extend(inst.collect())

    newitems = []
    for item in items:
        if item.parent.cls in rebuilt_items:
            newitems.extend(rebuilt_items[item.parent.cls])
            rebuilt_items[item.parent.cls][:] = []
        else:
            newitems.append(item)

    # seems like the functions attached to a test class aren't sorted already?
    # is that true and why's that? (when using unittest, they're sorted)
    items[:] = sorted(newitems, key=lambda item: (
        item.parent.parent.parent.name,
        item.parent.parent.name,
        item.name
    ))
pytestplugin.py 文件源码 项目:oa_qian 作者: sunqb 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):

    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            name.startswith("test_") and \
            isinstance(collector, pytest.Instance):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:chihu 作者: yelongyu 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:chihu 作者: yelongyu 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:ShelbySearch 作者: Agentscreech 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:ShelbySearch 作者: Agentscreech 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:Price-Comparator 作者: Thejas-1 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:Price-Comparator 作者: Thejas-1 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
fixtures.py 文件源码 项目:sslstrip-hsts-openwrt 作者: adde88 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def pytest_namespace():
    scopename2class.update({
        'class': pytest.Class,
        'module': pytest.Module,
        'function': pytest.Item,
    })
    return {
        'fixture': fixture,
        'yield_fixture': yield_fixture,
        'collect': {'_fillfuncargs': fillfixtures}
    }
fixtures.py 文件源码 项目:sslstrip-hsts-openwrt 作者: adde88 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def cls(self):
        """ class (can be None) where the test function was collected. """
        clscol = self._pyfuncitem.getparent(pytest.Class)
        if clscol:
            return clscol.obj
pytestplugin.py 文件源码 项目:Flask-NvRay-Blog 作者: rui7157 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:Flask-NvRay-Blog 作者: rui7157 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:Callandtext 作者: iaora 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:Callandtext 作者: iaora 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
pytestplugin.py 文件源码 项目:python_ddd_flask 作者: igorvinnicius 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            isinstance(collector, pytest.Instance) and \
            plugin_base.want_method(collector.cls, obj):
        return pytest.Function(name, parent=collector)
    else:
        return []
fixtures.py 文件源码 项目:godot-python 作者: touilleMan 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def pytest_namespace():
    scopename2class.update({
        'class': pytest.Class,
        'module': pytest.Module,
        'function': pytest.Item,
    })
    return {
        'fixture': fixture,
        'yield_fixture': yield_fixture,
        'collect': {'_fillfuncargs': fillfixtures}
    }
fixtures.py 文件源码 项目:godot-python 作者: touilleMan 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def cls(self):
        """ class (can be None) where the test function was collected. """
        clscol = self._pyfuncitem.getparent(pytest.Class)
        if clscol:
            return clscol.obj
fixtures.py 文件源码 项目:godot-python 作者: touilleMan 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def pytest_namespace():
    scopename2class.update({
        'class': pytest.Class,
        'module': pytest.Module,
        'function': pytest.Item,
    })
    return {
        'fixture': fixture,
        'yield_fixture': yield_fixture,
        'collect': {'_fillfuncargs': fillfixtures}
    }
fixtures.py 文件源码 项目:godot-python 作者: touilleMan 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def cls(self):
        """ class (can be None) where the test function was collected. """
        clscol = self._pyfuncitem.getparent(pytest.Class)
        if clscol:
            return clscol.obj
classes.py 文件源码 项目:pytest-relaxed 作者: bitprophet 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def makeitem(self, name, obj):
        # NOTE: no need to modify collect() this time, just mutate item
        # creation.
        # TODO: can't we redo SpecClass the same way? And SpecModule??
        item = super(SpecInstance, self).makeitem(name, obj)
        # Replace any Class objects with SpecClass; this will automatically
        # recurse.
        # TODO: can we unify this with SpecModule's same bits?
        if isinstance(item, Class):
            item = SpecClass(item.name, item.parent)
        return item


问题


面经


文章

微信
公众号

扫码关注公众号