在 Robot Framework 中的 Suite Teardown 之前解析 output.xml

Parse output.xml before Suite Teardown in Robot Framework

我想在 Robot Framework 中读取来自 output.xml 的统计数据,并将它们作为通知发送到 MS Teams 中。我的计划是通过在 Suite Teardown 中解析 output.xml 来做到这一点,但我现在注意到该文件在 Suit Teardown 之后是空白的,因此其中没有要解析的信息。我的问题是在套件拆解完成之前是否有任何方法可以获取这些数据?我注意到 Robot Framework 的 Listener API,是否可以从 python 访问它?

这是我的 python 代码,基本上是:

import xml.etree.ElementTree as ET

class TeamsNotifications(object):

    @staticmethod
    def get_stats():
        tree = ET.parse('output.xml')
        root = tree.getroot()

        stats_list = root.find('.//statistics//total')[1].attrib
        return stats_string

    def send_notification(self):
        stats = self.get_stats()
        # *send 'stats' as teams-notification*

还有我的机器人代码:

*** Settings ***
Library     x.TeamsNotifications

Suite Teardown  After suite teardown

*** Test Cases ***
----

*** Keywords ***
After suite teardown
    send notification

您可以设置您的 library to be a listener in addition to providing keywords. You can get the statistics in the _end_suite method when the suite id is "s1". The passed-in result object 拥有用于生成报告的所有数据。

这是一个简单的例子:

class ExampleLibrary:
    ROBOT_LISTENER_API_VERSION = 3
    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self

    def _end_suite(self, suite, result):
        if suite.id == "s1":
            # the root suite has ended
            print(result.stat_message)

您可以从 result.statistics 中提取数据以进行通知,而不是打印 result.stat_message