Robot Framework中Rest API响应incidentDate字段非空验证方法问询
Robot Framework 验证API响应日期字段存在且非空
前置说明
Selenium是用于UI自动化测试的工具,测试Rest API更适合用RequestsLibrary,先通过以下命令安装依赖:
pip install robotframework-requests
测试用例实现
单条数据验证
*** Settings *** Library RequestsLibrary Library BuiltIn *** Test Cases *** Check Incident Date Field Validity # 建立API会话,替换为你的API基础地址 Create Session api_session https://your-api-domain.com # 发送请求,替换为实际接口路径 ${api_response}= Get Request api_session /your-target-endpoint # 先验证请求返回正常 Should Be Equal As Integers ${api_response.status_code} 200 # 把响应文本转成JSON对象 ${response_json}= To Json ${api_response.text} # 从数组中取出第一个事故对象 ${incident_item}= Get From List ${response_json['claimIncident']} 0 # 验证字段存在 Dictionary Should Contain Key ${incident_item} incidentDate # 验证字段值非空 ${date_value}= Get From Dictionary ${incident_item} incidentDate Should Not Be Empty ${date_value}
多条数据批量验证
如果claimIncident是包含多个对象的数组,可通过循环批量验证:
*** Settings *** Library RequestsLibrary Library BuiltIn *** Test Cases *** Check All Incident Date Fields Create Session api_session https://your-api-domain.com ${api_response}= Get Request api_session /your-target-endpoint Should Be Equal As Integers ${api_response.status_code} 200 ${response_json}= To Json ${api_response.text} ${incident_list}= Get From Dictionary ${response_json} claimIncident # 遍历数组里的每个事故对象 :FOR ${incident} IN @{incident_list} \ Dictionary Should Contain Key ${incident} incidentDate \ ${date_val}= Get From Dictionary ${incident} incidentDate \ Should Not Be Empty ${date_val}
关键步骤解释
Create Session:创建可复用的API会话,减少重复配置To Json:将接口返回的文本格式响应转换为可操作的JSON对象Dictionary Should Contain Key:强制校验目标字段在对象中存在Should Not Be Empty:确保字段有实际有效值,而非空字符串或null
内容的提问来源于stack exchange,提问作者Naveen
相关产品推荐
相关产品推荐

