api - 如何从 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的描述
解决方案
看看这些例子。我做了类似的事情来生成测试计划描述。我试图根据您的要求调整我的代码,这可能对您有用。
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()
推荐阅读
- python - 如何使 QMenu 可滚动并同时修复它的位置?
- sql - 获取所有父行和每行后跟其子行
- git - 为什么我的 Git 提交失败并出现“重复父级”错误?
- microsoft-graph-toolkit - 直接显示人卡,无需悬停
- c++ - 如何使用类参数编写函数
- html - 将 TinyMCE 本地图像上传到托管 TinyMCE 的不同服务器:所有“http”引用都丢失
- c# - Microsoft.Toolkit.Forms.UI.Controls.WebView:如何设置 Cookie
- qnamaker - QNAmaker:如何为子问题启用仅上下文?
- wordpress - 购买前验证 Wocommerce 客户
- php - WooCommerce 订单通知电子邮件取决于结帐字段选择