Robotframework 如何使用Robot框架获得每个测试结果一行?

Robotframework 如何使用Robot框架获得每个测试结果一行?,robotframework,Robotframework,我想从Robot框架运行中获取测试用例结果,并将这些结果导入其他工具(ElasticSearch、ALM工具等) 为此,我希望能够生成一个文本文件,每个测试一行。以下是一个以线管分隔的示例: testcase name | time run | duration | status 我还想添加其他字段,但这些是基本字段。谢谢你的帮助。我一直在看robot.result,但还没弄明白。如果我这样做了,我会把答案贴在这里 谢谢,使用普通的xml解析库,output.xml文件非常容易解析 下面是一个

我想从Robot框架运行中获取测试用例结果,并将这些结果导入其他工具(ElasticSearch、ALM工具等)

为此,我希望能够生成一个文本文件,每个测试一行。以下是一个以线管分隔的示例:

testcase name | time run | duration | status
我还想添加其他字段,但这些是基本字段。谢谢你的帮助。我一直在看robot.result,但还没弄明白。如果我这样做了,我会把答案贴在这里


谢谢,

使用普通的xml解析库,output.xml文件非常容易解析

下面是一个简单的例子:

from __future__ import print_function
import xml.etree.ElementTree as ET
from datetime import datetime

def get_robot_results(filepath):

    results = []
    with open(filepath, "r") as f:
        xml = ET.parse(f)
        root = xml.getroot()
        if root.tag != "robot":
            raise Exception("expect root tag 'robot', got '%s'" % root.tag)

    for suite_node in root.findall("suite"):
        for test_node in suite_node.findall("test"):
            status_node = test_node.find("status")

            name = test_node.attrib["name"]
            status = status_node.attrib["status"]
            start = status_node.attrib["starttime"]
            end = status_node.attrib["endtime"]
            start_time = datetime.strptime(start, '%Y%m%d %H:%M:%S.%f')
            end_time = datetime.strptime(end, '%Y%m%d %H:%M:%S.%f')
            elapsed = str(end_time-start_time)

            results.append([name, start, elapsed, status])

    return results


if __name__ == "__main__":
    results = get_robot_results("output.xml")
    for row in results:
        print(" | ".join(row))

Bryan说得对,使用标准xml解析模块很容易解析Robot的output.xml。或者,您可以使用Robot自己的结果解析模块以及从中获得的模型:

from robot.api import ExecutionResult, SuiteVisitor


class PrintTestInfo(SuiteVisitor):

    def visit_test(self, test):
        print('{} | {} | {} | {}'.format(test.name, test.starttime,
                                         test.elapsedtime, test.status))


result = ExecutionResult('output.xml')
result.suite.visit(PrintTestInfo())

有关上面使用的api的更多详细信息,请参见。

不要像XML解析器那样简单(仅当您不知道如何使用robot.api:)但一旦您知道并看到此代码是多么干净和优雅,它就会变得简单。加上更容易扩展。