首页 > 解决方案 > 如何从 Robot 框架测试用例中提取 [Documentation] 文本

问题描述

我正在尝试将[Documentation]部分的内容提取为字符串,以便与Python 脚本中的其他部分进行比较。有人告诉我使用机器人框架 API https://robot-framework.readthedocs.io/en/stable/ 来提取,但我不知道如何。

但是,我需要使用 3.1.2 版

例子:

*** Test Cases ***
ATC Verify that Sensor Battery can enable and disable manufacturing mode
    [Documentation]    E1: This is the description of the test 1
    ...                E2: This is the description of the test 2
    [Tags]    E1    TRACE{Trace_of_E1}
    ...       E2    TRACE{Trace_of_E2}

提取字符串为

E1:这是测试1的描述

E2:这是测试2的描述

标签: apitestingautomated-testsrobotframework

解决方案


看看这些例子。我做了类似的事情来生成测试计划描述。我试图根据您的要求调整我的代码,这可能对您有用。

import os
import re
from robot.api.parsing import (
    get_model, get_tokens, Documentation, EmptyLine, KeywordCall,
    ModelVisitor, Token
)

class RobotParser(ModelVisitor):
    def __init__(self):
        # Create object with remarkup_text to store formated documentation
        self.text = ''


    def get_text(self):
        return self.text

    def visit_TestCase(self, node):
        # The matched `TestCase` node is a block with `header` and
        # `body` attributes. `header` is a statement with familiar
        # `get_token` and `get_value` methods for getting certain
        # tokens or their value.
        


        for keyword in node.body:
            # skip empty lines
            if keyword.get_value(Token.DOCUMENTATION) == None:
                continue
            self.text += keyword.get_value(Token.ARGUMENT)

    def visit_Documentation(self,node):
        # The matched "Documentation" node with value
        self.remarkup_text += node.value + self.new_line 

    def visit_File(self, node):
        # Call `generic_visit` to visit also child nodes.       
        return self.generic_visit(node)

if __name__ == "__main__":
    path = "../tests"
    for filename in os.listdir(path):
        if re.match(".*\.robot", filename):
            model = get_model(os.path.join(path, filename))
            robot_parser = RobotParser()
            robot_parser.visit(model)
            text=robot_parser._text()

推荐阅读