RequestsLibrary 基于requests针对robot frmework开发的一个库,用于HTTP接口自动化测试.
项目地址: https://github.com/bulkan/robotframework-requests
本章重点:
:分析robo framework系统关键字
:自己动手开发系统关键字
接口测试用例
*** Settings ***
Library RequestsLibrary
Library Collections
*** Test Cases ***
testget 获取get请求
${payload}= ----> Create Dictionary ----> eid=1
变量 通过字典 参数
Create Session ----> event ----> http://127.0.0.1:8000/api
定义session event = http://127.0.0.1:8000/api (本地api)
${r}= ---->get Request----> event----> /get_event_list/--->params=${payload}
${r}=接口返回值(发送 请求通过event以及拼上 /get_event_list/ ,然后传参 ${payload} )
Should Be Equal As Strings---->${r.status_code} ---->200
判断状态码是不是200
log ----> ${r.json()}
打印接口信息以json方式
${dict}---->set variable ----> ${r.json()}
把json格式转换成字典
#断言结果
${msg}----> get from Dictionary----> ${dict}----> message
获取接口${dict}信息
Should Be Equal----> ${msg}----> success
判断信息是否成功
${sta}----> get from Dictionary ---->${dict} ---->status
获取接口返回的状态码(不是请求的状态码)
${status} ---->Evaluate----> int(200)
把状态码转换成int类型
Should Be Equal----> ${sta}----> ${status}
判断状态码是不是200
testpost 获取post请求
Create Session event http://127.0.0.1:8000/api
创建session 接口的根目录
&{headers} create Dictionary Content-Type=application/x-www-form-urlencoded
创建字典 描述了post请求的格式 type格式
&{payload}= create Dictionary eid=1
创建post接口的参数
${r}= post Request event /add_event/ data=${payload} headers=$headers}
把post请求添加进去
Should Be Equal As Strings ${r.status_code} 200
判断状态码是不是200
log ${r.json()}
打印接口信息以json方式
#断言结果
${msg} get from Dictionary ${dict} message
获取接口${dict}信息
Should Be Equal ${msg} parameter error
参数错误 信息
${sta} get from Dictionary ${dict} status
获取接口里面的状态码
${status} Evaluate int(10021)
把状态码转换成int类型
Should Be Equal ${sta} ${status}
判断状态码是不是10021