当产生pytest.File或pytest.Item实例时,我如何指出这些需要夹具?

时间:2017-03-09 08:44:19

标签: pytest

鉴于以下模拟:

import pytest


class DummyFile(pytest.File):

    def collect(self):
        yield DummyItem(self, self)


class DummyItem(pytest.Item):
    def __init__(self, pytest_file, parent):
        super(DummyItem, self).__init__(pytest_file.name, parent)
        self.pytest_file = pytest_file

    def runtest(self):
        ...

def pytest_collect_file(self, parent, path):
    if path.ext == ".rst":
        return DummyFile(path, parent)

如何指出DummyFileDummyItem实例需要特定的灯具?

如何从DummyItem内的DummyFile及其父DummyItem.runtest获取这些灯具?

1 个答案:

答案 0 :(得分:1)

很遗憾,截至2017年8月,还没有正式的API来访问pytest.Item子类的灯具,请参阅此Github issue。该问题的海报有Github gist,显示您必须使用的内部API:

fm = self.session._fixturemanager
names_closure, arg2fixturedefs = fm.getfixtureclosure(names, self)
fixtureinfo = FuncFixtureInfo(names, names_closure, arg2fixturedefs)
self._fixtureinfo = fixtureinfo
self.funcargs = {}
self._request = fixtures.FixtureRequest(self)