如何在Robot Framework的Variables段定义字典列表?
Robot Framework 2.9.2 字典列表定义方案
Robot Framework 2.9.2版本的Variables部分不支持直接用Python语法定义字典列表,需要通过以下两种变通方式实现需求:
方法一:通过关键字在初始化阶段构建
把字典列表的创建逻辑放到Test Setup或专用关键字中,再通过Set Suite Variable将其设为全局可用变量:
*** Test Cases *** Example Test [Setup] Initialize List Of Dicts Example Keyword *** Keywords *** Initialize List Of Dicts ${dict1}= Create Dictionary name=Alice age=25 ${dict2}= Create Dictionary name=Bob age=30 ${dict3}= Create Dictionary name=Charlie age=35 @{list_of_dicts}= Create List ${dict1} ${dict2} ${dict3} Set Suite Variable @{list_of_dicts} Example Keyword : FOR ${dict} IN @{list_of_dicts} \ Log ${dict['name']} is ${dict['age']} years old
方法二:通过Python模块导入变量
- 创建一个Python文件(比如
variables.py),在里面定义字典列表:
list_of_dicts = [ {'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35} ]
- 在Robot文件的Settings部分导入这个Python变量文件:
*** Settings *** Variables variables.py *** Keywords *** Example Keyword : FOR ${dict} IN @{list_of_dicts} \ Log ${dict['name']} is ${dict['age']} years old
这两种方式都能实现你想要的字典列表遍历效果,其中方法二更贴近你最初期望的“在变量段定义”的使用体验。
内容的提问来源于stack exchange,提问作者Rafael Nicolay
相关产品推荐
相关产品推荐

