HttpRunner2.X开源接口测试框架学习(二):yaml格式测试用例编写

HttpRunner测试用例的基本结构

  • 每个YAML/JSON文件对应一个测试用例(testcase)

  • 每个测试用例为一个list of dict结构,其中可能包含全局配置项(config)和若干个测试步骤(test)

  • config为全局配置项,作用域为整个测试用例

  • test对应单个测试步骤,作用域仅限于本身

  • 变量作用域以config为主,config如果没有设置,就使用test中设置的变量

congfig参数说明

 

 

 备注:最新用export输出参数,output已过时

test参数说明

 

request关键词中包括http请求中的详细内容:

  • headers:请求头部信息
  • method:请求方式
  • url:请求地址
  • host:请求主机地址
  • params:GET请求参数
  • data:表单形式的参数
  • json:json格式的参

一、基础栗子

get请求举例说明

以打开新梦想首页为例

host:http://www.hnxmxit.com

url:/

请求方式:get

 使用httprunner进行接口测试步骤:

1、打开pycharm,新建一个httprunner项目,新建一个test_demo.yaml文件,内容如下:

#不带参数的get请求
- config:
    name: 验证能否打开新梦想主页
    base_url: http://www.hnxmxit.com

- test:
    name: open hnxmxit mainpage api
    request:
      url: /
      method: GET
    validate:
      - eq: ['status_code',200]

 2、在pycharm,终端窗口执行如下命令 :hrun yaml文件名

 

3、执行成功后,会自动生成一个报告,查看报告:

二、模拟带参数的get请求

#模拟带参数的get请求
- config:
    name: '验证能否获取token值'
    base_url: 'https://api.weixin.qq.com'

- test:
    name: 'get access token'
    request:
      url: '/cgi-bin/token'
      method: GET
      params:
        grant_type: 'client_credential'
        appid: 'wx0ebbdf4a197121'
        secret: 'b876eeb2af99cc6623995201168e702f'
    validate:
      - eq: ['status_code',200]
      - eq: [content.expires_in,7200]    #content表示响应结果为json的响应数据


三、模拟请求头

#模拟请求头
- config:
    name: '验证百度搜索是否正确'
    base_url: 'https://www.baidu.com'

- test:
    name: '百度搜索请求'
    request:
      url: '/s'
      method: GET
      headers:
        User-Agent: 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Mobile Safari/537.36'
        Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
        Accept-Encoding: 'gzip, deflate, br'
        Accept-Language: 'zh-CN,zh;q=0.9'
      params:
        wd: 'newdream01'
    validate:
      - eq: ['status_code',200]

四、模拟POST请求

#模拟post请求
- config:
    name: '验证微信开发平台中,用户标签管理修改标签接口能否成功执行'
    base_url: 'https://api.weixin.qq.com'

- test:
    name: '修改标签接口'
    request:
      url: '/cgi-bin/tags/update'
      method: POST
      headers:
        Content-Type: 'application/json'
      params:
        access_token: '46_oMQuTV1IE2aTFeGKMHXHSNseS63bwEMxSyU7MrVYvOqXF1y5hnQvgopuZjBSqKBIGVrCaTlQEAeAAf-EtaGWahHTeEzFU-cNZ3_EgT7Xlbwx7rOFW9OTusypC9lIthaIn0Ooq60XJ09wrFYlJMIgAHAVYU'
      json: {   "tag" : {     "id" : 101,     "name" : "newdream123"   } }
    validate:
      - eq: ['status_code',200]
      - eq: [content.errcode,0]

五、变量作用域:变量作用域以config为主,config如果没有设置,就使用test中设置的变量

#变量作用域:变量作用域以config为主,config如果没有设置,就使用test中设置的变量
- config:
    name: '验证能否获取token值'
    base_url: 'https://api.weixin.qq.com'
    variables:
      - grant_type: 'client_credential'
          - appid: 'wx0ebbdf4a197121'

- test:
    name: 'get access token'
    variables: #变量作用域以config为主,config如果没有设置,就使用test中设置的变量
        - grant_type: 'client_credential'
        - appid: 'wx0ebbdf4a197121'
    request:
      url: '/cgi-bin/token'
      method: GET
      params:
        grant_type: $grant_type
        appid: $appid
        secret: 'b876eeb2af99cc6623995201168e702f'
    validate:
      - eq: ['status_code',200]
      - eq: [content.expires_in,7200]

六、test中extract参数的应用1:响应结果为HTML类型时,采用正则表达式提取

#test中extract参数的应用1:响应结果为HTML类型时,采用正则表达式提取
- config:
    name: 验证能否打开新梦想主页
    base_url: http://www.hnxmxit.com

- test:
    name: open hnxmxit mainpage api
    request:
      url: /
      method: GET
    extract:
      - code: 'status_code'
      - reason: reason
      - time: elapsed.microseconds
      - Type: headers.Content-Type
      - title: <title>(.+?)</title>
      - s: <img src="/style/img/iob8(.+?)" alt="广州汇思信息科技有限公司" />
    validate:
      - eq: [$code,200]
      - eq: [$reason,'OK']
      - lt: [$time,6000000]
      - str_eq: [$Type,'text/html; charset=utf-8']
      - str_eq: [$s,'.png']
      - str_eq: [$title,'新梦想培训_软件测试培训_Java培训班_IT技术培训_软件测试视频课程_IT培训机构']

七、test中extract参数的应用2:响应结果为json时,采用.运算符的方式提取

#test中extract参数的应用2:响应结果为json时,采用.运算符的方式提取
- config:
    name: '验证能否获取公众号已创建的标签'
    base_url: 'https://api.weixin.qq.com'

- test:
    name: 'get all user tag api'
    request:
      url: '/cgi-bin/tags/get'
      method: GET
      params:
        access_token: '46_rBJ4f-vSG9o21uwyqtSa-hGr2Sf9Pr0b49vDbsq3e7sdmIdlkYsCHTt6OInJLp1egq5BfOUgDaLn4T6Z_uRvSDy92n8UaNw8b0ARLbSgdCrJmEGGkFFtcKJk7B9WtHCSAND3nZqe0x2FhqRBVGZgAFAALE'
    extract:
      - id: content.tags.2.id
      - classname: content.tags.2.name
    validate:
      - eq: [$id,101]
      - eq: [$classname,'newdream123']

八、test中validate 断言两种写法

#test中validate 断言两种写法
- config:
    name: 验证能否打开新梦想主页
    base_url: http://www.hnxmxit.com

- test:
    name: open hnxmxit mainpage api
    request:
      url: /
      method: GET
    extract:
      - code: 'status_code'
          - reason: reason
    validate:
      - eq: [$code,200]
      - {'check':$reason,'comparator':'str_eq','expect':'OK'}

九、config output 在terminal打印参数内容

#config output 在terminal打印参数内容,可输出的参数包括公共的variable和extract参数
- config:
    name: 验证能否打开新梦想主页
    base_url: http://www.hnxmxit.com
    output:
      - code

- test:
    name: open hnxmxit mainpage api
    request:
      url: /
      method: GET
    extract:
      - code: 'status_code'
    validate:
      - eq: [$code,200]

十、httprunner 实现关联

#httprunner 实现关联
- config:
    name: '验证能否获取token值'
    base_url: 'https://api.weixin.qq.com'

- test:
    name: 'get access token'
    request:
      url: '/cgi-bin/token'
      method: GET
      params:
        grant_type: 'client_credential'
        appid: 'wx0ebbdf4a197121db'
        secret: 'b876eeb2af99cc6623995201168e702f'
    extract:
      - token: content.access_token
    validate:
      - eq: ['status_code',200]
      - eq: [content.expires_in,7200]

- test:
    name: 'delete tag'
    request:
      url: '/cgi-bin/tags/delete'
      method: POST
      headers:
        Content-Type: 'application/json'
      params:
        access_token: $token
      json: {"tag":{"id":103}}
    validate:
      - eq: [content.errcode,0]
原文地址:https://www.cnblogs.com/lvhuayan/p/14891745.html