You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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模块导入变量

  1. 创建一个Python文件(比如variables.py),在里面定义字典列表:
list_of_dicts = [
    {'name': 'Alice', 'age': 25},
    {'name': 'Bob', 'age': 30},
    {'name': 'Charlie', 'age': 35}
]
  1. 在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 04:45:43