我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pytest.Instance()。
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*", ])
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*", ])
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 []
def matchkeyword(colitem, keywordexpr): """Tries to match given keyword expression to given collector item. Will match on the name of colitem, including the names of its parents. Only matches names of items which are either a :class:`Class` or a :class:`Function`. Additionally, matches on names in the 'extra_keyword_matches' set of any item, as well as names directly assigned to test functions. """ mapped_names = set() # Add the names of the current item and any parent items import pytest for item in colitem.listchain(): if not isinstance(item, pytest.Instance): mapped_names.add(item.name) # Add the names added as extra keywords to current or parent items for name in colitem.listextrakeywords(): mapped_names.add(name) # Add the names attached to the current function through direct assignment if hasattr(colitem, 'function'): for name in colitem.function.__dict__: mapped_names.add(name) mapping = KeywordMapping(mapped_names) if " " not in keywordexpr: # special case to allow for simple "-k pass" and "-k 1.3" return mapping[keywordexpr] elif keywordexpr.startswith("not ") and " " not in keywordexpr[4:]: return not mapping[keywordexpr[4:]] return eval(keywordexpr, {}, mapping)
def pytest_runtest_setup(item): if is_potential_nosetest(item): if isinstance(item.parent, pytest.Generator): gen = item.parent if not hasattr(gen, '_nosegensetup'): call_optional(gen.obj, 'setup') if isinstance(gen.parent, pytest.Instance): call_optional(gen.parent.obj, 'setup') gen._nosegensetup = True if not call_optional(item.obj, 'setup'): # call module level setup if there is no object level one call_optional(item.parent.obj, 'setup') #XXX this implies we only call teardown when setup worked item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item)
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 []
def pytest_pycollect_makeitem(self, collector, name, obj): tests_dict = self.tests_dict if inspect.isfunction(obj) and name.startswith("test_") and isinstance(collector, pytest.Instance): fully_qualified_name = '.'.join([obj.__module__, obj.__qualname__]) for test_id in tests_dict.keys(): if fully_qualified_name == re.sub('\[.*?\]$', '', test_id): return return []
def collect(self): items = super(SpecClass, self).collect() collected = [] # Replace Instance objects with SpecInstance objects that know how to # recurse into inner classes. # TODO: is this ever not a one-item list? Meh. for item in items: item = SpecInstance(name=item.name, parent=item.parent) collected.append(item) return collected
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) items[:] = [ item for item in items if isinstance(item.parent, pytest.Instance) and not item.parent.parent.name.startswith("_")] 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 ))