record_xml_property不适用于xdist

时间:2017-02-23 13:42:26

标签: python pytest xdist

我正在我的测试中积极使用record_xml_property fixture,但遗憾的是它不适用于xdsit插件。我试图实现一种解决方法。我没有直接向记者添加属性,而是将它们放到slaveoutput:

@pytest.fixture
def record_xml_property(request):
    xml = getattr(request.config, "_xml", None)

    if xml is not None:
        node_reporter = xml.node_reporter(request.node.nodeid)
        return node_reporter.add_property

    else:
        def add_property_xdist(name, value):
            request.node.config.slaveoutput.update({'properties': {name: value}})

        return add_property_xdist

之后我想在pytest_runtest_logreport hook:

中将此属性添加到报告中
@pytest.hookimpl(tryfirst=True)
def pytest_runtest_logreport(report):
    if report.when != 'teardown':
        return

    node = getattr(report, 'node', None)

    if not node:
        return

    xml = getattr(node.config, '_xml', None)

    if not xml:
        return

    node_reporter = xml.node_reporter(report)
    slaveoutput = getattr(node, 'slaveoutput', None)

    if not slaveoutput:
        return

    node_properties = slaveoutput.get('properties', {})

    for key, value in node_properties.items():
        node_reporter.add_property(key, value)

但问题是,当执行SlaveController挂钩时,{0}}对象中的slaveoutput通常不可用。它在pytest_runtest_logreport中可用,pytest_testnodedown通过SlaveInteractor事件发送,但此时报告已经完成。有没有办法先从slave节点获取slaveoutput?

1 个答案:

答案 0 :(得分:1)

最后我决定避免使用slaveoutput。我在想一些涉及使用共享目录的解决方案,但最后我决定在主节点和从节点之间使用execnet通道。不幸的是,似乎xdist的设计不允许容易地扩展主节点和从节点之间的通信。我不得不做一些猴子修补,代码重复和一些其他讨厌的事情,我不喜欢。但至少它有效。 这是我的解决方案:

@pytest.fixture
def record_xml_property(request):
    """
    Overrides original record_xml_property fixture from junitxml plugin
    """
    xml = getattr(request.config, "_xml", None)

    if xml is not None:
        node_reporter = xml.node_reporter(request.node.nodeid)
        return node_reporter.add_property
    else:
        pluginmanager = request.node.config.pluginmanager
        plugins = pluginmanager.get_plugins()
        slave_interactor = None

        for plugin in plugins:
            if plugin.__class__.__name__ == 'SlaveInteractor':
                slave_interactor = plugin

        if slave_interactor:
            # if test is running in slave node
            def add_property_xdist(name, value):
                slave_interactor.sendevent("record_xml_property", property=(name, value), nodeid=request.node.nodeid)

            return add_property_xdist

    def add_property_noop(name, value):
        pass

    return add_property_noop


def pytest_configure_node(node):
    xml = getattr(node.config, '_xml', None)

    if xml:
        # monkeypatching SlaveController right after it is created
        # process_from_remote is a callback function which is executed on channel event
        original_process_from_remote = node.process_from_remote

        def process_from_remote(self, eventcall):
            try:
                if eventcall != self.ENDMARK:
                    eventname, kwargs = eventcall

                    # In case of record_xml_property event, process it here. Otherwise process it in original
                    # process_from_remote function
                    if eventname == "record_xml_property":
                        self.log("recording xml property %s(%s)" % (eventname, kwargs))

                        # Report is not yet exists. I should use fake one to get proper reporter
                        class Fakereport:
                            def __init__(self, node, nodeid):
                                self.node = node
                                self.nodeid = nodeid

                        node_reporter = xml.node_reporter(Fakereport(self, kwargs['nodeid']))
                        name, value = kwargs['property']
                        node_reporter.add_property(name, value)
                        return
            except KeyboardInterrupt:
                # should not land in receiver-thread
                raise
            except:
                # the same except as in original process_from_remote method
                excinfo = py.code.ExceptionInfo()
                py.builtin.print_("!" * 20, excinfo)
                self.config.pluginmanager.notify_exception(excinfo)

            return original_process_from_remote(eventcall)

        node.process_from_remote = types.MethodType(process_from_remote, node)