Python-Robot Framework API自动化报错:AttributeError: 'list' object has no attribute 'items'
问题排查:Robot Framework API测试报错AttributeError: 'list' object has no attribute 'items'
报错原因
你的代码中headers=@{header}传递的是列表类型变量,但RequestsLibrary的POST On Session关键字要求headers参数必须是字典类型,列表不存在items()方法,因此触发该错误。
修复方案
将headers的定义从列表改为字典即可解决问题:
1. 修改变量定义
把原来的列表变量@{header}替换为字典变量${header},两种写法任选其一:
# 写法1:用Create Dictionary关键字定义 *** Variables *** ${base_url} https://reqres.in ${header} Create Dictionary Content-Type=application/json; charset=utf-8 # 写法2:直接定义字典字符串 ${header} {"Content-Type": "application/json; charset=utf-8"}
2. 调整测试用例中的调用
将headers=@{header}改为headers=${header}:
${response}= POST On Session mysession api/users data=${body} headers=${header}
完整修复后的代码
*** Settings *** Library RequestsLibrary Library JSONLibrary Library Collections *** Variables *** ${base_url} https://reqres.in ${header} Create Dictionary Content-Type=application/json; charset=utf-8 *** Test Cases *** POST_userInfo Create Session mysession ${base_url} ${body}= Create Dictionary name=Prad job=leader ${response}= POST On Session mysession api/users data=${body} headers=${header} Log To Console ${response.status_code} Log To Console ${response.content} Should Be Equal As Strings ${response.status_code} 201
内容的提问来源于stack exchange,提问作者Pradnya Deshpande
相关产品推荐
相关产品推荐

