RobotFramework基础(2)-一个接口测试案例

1.业务场景

利用RobotFramework实现一个简单的接口测试实践。

2.知识点梳理

(1)接口测试

(2)封装

(3)断言

3.说明

思维导图梳理了接口测试环境的要求和一个简单的用例实践。

4.思维导图

5.robot文件 

 1 *** Settings ***
 2 Documentation    http请求Demo
 3 Library  Collections
 4 Library  RequestsLibrary
 5 
 6 *** Variables ***
 7 ${host}    http://httpbin.org
 8 
 9 *** Test Cases ***
10 Post 请求Demo
11     [Tags]            Post    main
12     ${data}           Create Dictionary    name=Detector    age=18
13     create session    httpbin    ${host}
14     ${response}       post request   httpbin   /post      params=${data}
15     should be equal as integers    ${response.status_code}     200
16     ${resp}           to json   ${response.content}
17     should not be empty    ${resp["args"]}
18     dictionary should contain key  ${resp["headers"]}     Host
19     dictionary should contain value  ${resp["headers"]}     httpbin.org
20 
21 Get 请求Demo
22     [Tags]            Get
23     ${data}           Create Dictionary    name=Detector    age=18
24     create session    httpbin    ${host}
25     ${response}       get request   httpbin   /get      params=${data}
26     should be equal as integers    ${response.status_code}     200
27     ${resp}           to json   ${response.content}
28     should not be empty    ${resp["args"]}
29     dictionary should contain key  ${resp["headers"]}     Host
30     dictionary should contain value  ${resp["headers"]}     httpbin.org
simble_test_demo.robot

*** Settings ***中引入需要的库,*** Variables ***中定义全局变量,*** Test Cases ***中编写测试用例。

Post 请求Demo

行11:[Tags]用于存储标签,post和main都为标签

行12:创建字典{name=Detector,age=18}存放到data中

行13:创建了一个HTTP会话,并给它取一个别名httpbin

行14:执行post请求,请求的数据存放到response中

行15:断言response状态码等于200

行16:把二进制数据response.content转为json数据格式,存放到resp中

行17:断言resp["args"]不为空

行18:断言resp["headers"]中包含Host

行19:断言resp["headers"]中包含httpbin.org

 1 *** Settings ***
 2 Documentation    http请求Demo
 3 Library  Collections
 4 Library  RequestsLibrary
 5 
 6 *** Variables ***
 7 ${host}    http://httpbin.org
 8 *** Test Cases ***
 9 Package Post
10     [Tags]      packaged req
11     ${data}     Create Dictionary    name=Detector    age=18
12     ${resp}     POST Req             ${data}
13     should not be empty    ${resp["args"]}
14     dictionary should contain key  ${resp["headers"]}     Host
15     dictionary should contain value  ${resp["headers"]}     httpbin.org
16 
17 *** Keywords ***
18 POST Req
19     [Arguments]       ${data}
20     Create Session    api             ${host}
21     ${response}       Post Request    api    /post  params=${data}
22     should be equal as integers    ${response.status_code}     200
23     ${resp}           to json   ${response.content}
24     [Return]    ${resp}
simple_packaging_test_demo.robot

将post请求获取数据的过程进行封装到*** Keywords ***中,[Arguments]后面为要传入的数据,[Return]后面为要返回的数据。

 1 *** Test Cases ***
 2 packaged test
 3     [Template]  Packaged POST Req
 4     name       host     Content-Length=0
 5     name       host     name     Content-Length=0
 6 
 7 *** Keywords ***
 8 Packaged POST Req
 9     [Arguments]       ${data}         ${Hosts}      @{args}     &{HostValue}
10     Create Session    api             ${host}
11     ${response}       Post Request    api    /post  params=${data}
12     should be equal as integers    ${response.status_code}     200
13     ${resp}           to json   ${response.content}
14 #    log  ${resp}
15     dictionary_should_contain_sub_dictionary      ${resp["headers"]}       ${HostValue}
16     run keyword if    ${args}    log      ${args}
17     ...       ELSE    should not be empty     ${resp["args"]}
complex_packaging_test_demo.robot

将post请求数据的过程和断言都进行了封装,也就是将整个post请求功能都进行了封装。

6.参考链接

https://www.cnblogs.com/Detector/p/10176870.html

posted on 2020-09-09 00:45  ioYuki  阅读(409)  评论(0编辑  收藏  举报

导航