Robot框架中套件拆卸前解析output.xml

2024-06-08 19:27:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从Robot框架中的output.xml读取统计数据,并将它们作为通知发送给MS团队。我的计划是通过在suiteteardown中解析output.xml来实现这一点,但我现在注意到,在suiteteardown之前,该文件是空白的,因此其中没有要解析的信息。我的问题是,在套件拆卸完成之前,是否有任何方法可以获取这些数据?我注意到Robot框架的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

Tags: 代码框架sendtreeoutputgetdefas
1条回答
网友
1楼 · 发布于 2024-06-08 19:27:48

除了提供关键字外,还可以设置library to be a listener。当套件id为“s1”时,可以在_end_suite方法中获取统计信息。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

相关问题 更多 >