【问题标题】:How can I get a one line per test result with Robot Framework?如何使用 Robot Framework 获得每个测试结果一行?
【发布时间】:2017-12-12 23:55:27
【问题描述】:

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

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

testcase name | time run | duration | status

我会添加其他字段,但这些是基本字段。任何帮助表示赞赏。我一直在查看 robot.result http://robot-framework.readthedocs.io/en/3.0.2/autodoc/robot.result.html,但还没有弄清楚。如果/当我这样做时,我会在这里发布答案。

谢谢,

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    output.xml 文件很容易用普通的 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))
    

    【讨论】:

      【解决方案2】:

      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 的更多详细信息,请参阅http://robot-framework.readthedocs.io/

      【讨论】:

      • 不像 XML 解析器那么简单(仅当您不知道如何使用robot.api 时:)但是一旦您使用并看到这段代码是多么干净和优雅,右边就很简单复杂的一面。而且更容易扩展。
      猜你喜欢
      • 1970-01-01
      • 2016-01-24
      • 2020-10-07
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 2022-12-29
      • 2021-04-19
      相关资源
      最近更新 更多