Robot Framework:如何确保变量为字典列表(自动转换字典)
解决Robot Framework中${agent}变量格式统一问题
可以通过类型判断+条件赋值的方式实现需求,下面提供两种简洁的写法:
写法一:使用Set Variable If(推荐,更简洁)
利用Set Variable If的三元表达式特性,直接完成判断与赋值:
${agent}= Set Variable If ${agent} is instance of dict [${agent}] ${agent}
逻辑说明:
- 当
${agent}是字典类型时,将其包裹为列表[${agent}]重新赋值 - 若
${agent}已是字典列表,则直接保留原变量值
写法二:使用Run Keyword If
如果习惯分步逻辑,也可以用Run Keyword If实现:
${agent}= Run Keyword If ${agent} is instance of dict Set Variable [${agent}] ELSE Set Variable ${agent}
验证示例
- 初始值为单个字典:
${agent}={1:2}→ 执行后变为[{1:2}] - 初始值为字典列表:
${agent}=[{1:2}]→ 执行后保持[{1:2}]不变
内容的提问来源于stack exchange,提问作者Arokia Raj
相关产品推荐
相关产品推荐

