Robot Framework参数传递为字符串而非实际类型(列表、字典等)问题排查
Robot Framework参数传递异常问题排查
问题场景
测试用例代码:
${filter} = Create Filter with apps ${appList}
YAML文件声明${appList}值为["app1", "app2"]并加载到测试文件,关键字代码:
Create Filter with apps ${list} ${app_filter} = Generate app filters ${list}
对应Python函数:
def generate_app_filters(appList=[]): for app in appList: print(app)
预期输出打印app1和app2,但实际遍历打印字符串["app1", "app2"]的每个字符。
核心原因
Robot Framework加载YAML变量时,${appList}被解析为字符串类型而非列表类型,导致Python函数接收的是字符串"['app1', 'app2']",遍历字符串时会逐个输出字符。
解决方案
1. 修正YAML变量定义格式
确保YAML中列表的写法符合规范,让Robot Framework能自动识别为列表:
- 推荐换行式写法:
appList: - app1 - app2 - 紧凑写法(需保证YAML解析器支持):
appList: ["app1", "app2"]
2. 验证变量类型
在测试用例或关键字中添加日志,确认${appList}的实际类型:
Log Type ${appList} loglevel=DEBUG
若输出为str,说明加载时未解析为列表;若为list,则问题出在参数传递环节。
3. 强制转换为列表(临时修复)
若无法修改YAML加载逻辑,可在Robot Framework关键字中先转换参数类型:
Create Filter with apps ${list} ${actual_list} = Convert To List ${list} ${app_filter} = Generate app filters ${actual_list}
或者在Python函数中手动解析字符串:
import ast def generate_app_filters(appList): # 检查是否为字符串,若是则解析为列表 if isinstance(appList, str): try: appList = ast.literal_eval(appList) except (ValueError, SyntaxError): # 解析失败时的降级逻辑 appList = [] for app in appList: print(app)
4. 确认变量加载方式
确保测试文件正确通过Variables导入YAML文件,而非自定义脚本读取(自定义读取可能直接返回字符串):
*** Settings *** Variables path/to/your/vars.yaml
内容的提问来源于stack exchange,提问作者Yogendiran Barath
相关产品推荐
相关产品推荐

