一、Robot Framework接口测试介绍
接口测试,无非就是通过http请求需要测试的接口,不同接口的数据准备包括:url,domain,header,cookie,param/data等等。
返回值,一般是json/text/xml,我们要做的就是多了些数据转换操作,不同接口处理起来大同小异。
二、接口测试脚本示例
引用Library
requests json RequestsLibrary Collections SeleniumLibrary #要使用cookie的话需要打开浏览器
1、先创建resource,稍微封装一下
查询、更新CPS订单接口示例.txt
*** Settings *** Library requests Library json Library RequestsLibrary Library Collections Library SeleniumLibrary *** Keywords *** 接口查询CPS订单 [Arguments] ${pageIndex} ${pageSize} #API说明 http://localhost:777/HelpPage/Help/Api/GET-v2-Order-GetCPSOrderList ${apiurl} set variable /v2/Order/GetCPSOrderList ${params} set variable pageIndex=${pageIndex}&pageSize=${pageSize} ${fullurl} set variable ${apiurl}?${params} #comment 访问GET接口地址:${fullurl} log ................................................................................ log 访问GET接口地址:${fullurl} log ................................................................................ ${response} api接口请求 "get" ${apiurl} getparams=${params} ${jsonData} to json ${response.content} #${dict} get dictionary items ${jsonData} ${dict} set variable ${jsonData} log ................................................................................ log 返回结果Json:${dict} log ................................................................................ log data = ${dict["data"]} ${data} Get From Dictionary ${dict} data ${totalCount} get from dictionary ${data} totalCount ${cpsOrderList} get from dictionary ${data} cpsOrderList log ................................................................................ log 返回json解析数据:totalCount=${totalCount} cpsOrderList=${cpsOrderList} log ................................................................................ Delete All Sessions [Return] ${totalCount} ${cpsOrderList} # 返回总数量和订单列表 接口更新CPS订单 [Arguments] ${OrderId} ${CPSFee} ${CpsOrderStatus} #API说明 http://localhost:777/HelpPage/Help/Api/POST-v2-Order-UpdateCPSOrderStatus ${apiurl} set variable /v2/Order/UpdateCPSOrderStatus ${dictParams} Create Dictionary OrderId=${OrderId} CPSFee=${CPSFee} CpsOrderStatus=${CpsOrderStatus} ${listParams} Create List ${dictParams} log ................................................................................ log 参数:${listParams} log ................................................................................ ${response} api接口请求 "post" ${apiurl} postdata=${listParams} ${content} to json ${response.content} #${Success} set variable ${true} ${Success} Get From Dictionary ${content} success ${message} Get From Dictionary ${content} message log ................................................................................ log 返回结果:success=${Success} message=${message} log ................................................................................ Delete All Sessions Should Be True ${Success}==${true} [Return] ${Success} # 是否成功 api接口请求 [Arguments] ${method} ${url} ${postdata}=${None} ${getparams}=${None} [Documentation] api接口请求 comment 接口域名地址,不含http:// ${domain} set variable api.test.com comment 模拟ApigeeHeader安全验证串 ${token} set variable uTnm9QSxFpcAd= comment 组建header信息 ${headers} Create Dictionary token=${token} host=${domain} Content-Type=application/json comment 从浏览器取cookie,需要打开浏览器 #${cookies} get cookies #${cookies} set variable comment 下面两种方式可以连接字符串 #${httpDomain} set variable http://${domain} #${httpDomain} catenate SEPARATOR= http:// ${domain} comment 创建本次api请求的session会话信息 create session api http://${domain} ${headers} comment 最简单的get请求方式,也可以实现get请求,就是没地方加header参数 #${response} requests.get ${fullurl} comment 请求接口并返回响应结果,判断get就调用 Get Request请求接口,否则调用Post Request请求接口 ${response}= run keyword if ${method} == "get" Get Request api ${url} headers=${headers} ... params=${getparams} ... ELSE Post Request api ${url} headers=${headers} data=${postdata} [Return] ${response} # 接口返回内容
其实,最重要的就是3行:
${response}= run keyword if ${method} == "get" Get Request api ${url} headers=${headers} ... params=${getparams} ... ELSE Post Request api ${url} headers=${headers} data=${postdata}
2、创建 Suite,创建测试用例
*** Settings *** Resource ../../../Resource/接口/webapi/查询、更新CPS订单接口示例.txt Resource ../../../Resource/公用/DB.txt Resource ../../../Resource/MAG后台/公用.txt *** Test Cases *** 查询CPS订单内容、更新CPS订单 [Documentation] 1、执行数据库更新SQL ... 2、查询数据库并判断 ... 3、访问CPS订单接口 ... 4、开始通过接口循环更新CPS订单 ... 4.1 单个CPS订单更新结果判断 ... 5、循环更新CPS订单结束 comment 1、执行数据库更新SQL 执行SQLSERVER ${None} update orders set CpsOrderStatus=-2 where orderid in(2706802,2706801); comment 2、查询数据库并判断 ${queryresult} 查SQLSERVER ${None} select OrderId,CpsOrderStatus from orders where orderid in(2706802,2706801); ${resultcount}= Get Length ${queryresult} : FOR ${index} IN RANGE 0 ${resultcount} ${data} Get From List ${queryresult} ${index} ${orderid} convert to string ${data[0]} log ${data} log ${queryresult[${index}][0]} Should Be Equal As Integers 2 ${resultcount} comment 3、访问CPS订单接口 ${totalCount} ${cpsOrderList} 接口查询CPS订单 1 20 ${OrderId} set variable ${None} ${CPSFee} set variable ${None} ${CpsOrderStatus} set variable ${None} ${list} set variable ${cpsOrderList} #Convert To List log CPS未处理订单数量:${totalCount} comment 4、开始通过接口循环更新CPS订单 : FOR ${index} IN RANGE 0 ${totalCount} log 循环list索引:${index} ${data} Get From List ${list} ${index} run keyword if ${data}==${None} exit for loop ${OrderId} Get From Dictionary ${data} orderId ${CPSFee} set variable 0.2 ${CpsOrderStatus} Get From Dictionary ${data} cpsOrderStatus ${Success} 接口更新CPS订单 ${OrderId} ${CPSFee} ${CpsOrderStatus} comment 4.1 单个CPS订单更新结果判断 Should Be Equal As Strings&nb