J06_在 HttpRunner 中通过“测试用例集合”组织多个用例的执行

J06_在 HttpRunner 中通过“测试用例集合”组织多个用例的执行

 

 

测试用例的执行肯定不能每次只执行一个文件,这样效率过低,因此我们需要把多个测试用例组织起来一起运行。HttpRunner 提供了测试用例集合(testsuite)的概念,完成一次多个测试用例的运行。

 

测试集合(也叫测试套)的组织形式如下图所示,一个测试集合文件下可以引用多个测试用例文件,每个测试用例下面又可以包括多个测试步骤。这样只要运行测试集合文件,其下的测试用例会被一并执行。

 

 

 

这个过程的实现,我们以码云(Gitee) API 文档接口为例来演示

 

测试接口平台:码云 API 文档

测试接口1:获取单个分支

 

测试接口2:获取仓库的所有提交

  • https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoCommits

 

 

本次测试未使用“脚手架”项目组织规范,只是把所有的文件统一放到一个目录下:

  • reports:执行后自动生成的存放测试报告的目录
  • gitee_api_xxxxx: 被测接口文件。为每个接口单独定义一个文件,该文件可以单独执行,也可以被测试用例调用。
  • gitee_testxx_xx: 测试用例文件。对每个接口编写的测试用例,其中调用接口文件。
  • gitee_suite.yml: 测试集文件。整合所有的测试用例,并依次执行。

 

 

 

1. 首先完成 API 接口定义文件

  • 获取单个分支接口
  • 获取某个分支所有提交信息接口

 

获取单个分支接口文件: gitee_api_get_branch.yml

name: 获取仓库单个分支接口

base_url: https://gitee.com/api/v5/repos

variables:
  token: d66685......1e1f5
  owner: youling_zhulongcao
  repo: Test
  branch: master

request:
  url: /$owner/$repo/branches/$branch
  method: GET
  params:
    access_token: $token

extract:
  - author: content.commit.commit.author.name
 
validate:
  - eq: [status_code, 200]
  - eq: [$author, 悠灵]

 

 

获取某个分支所有提交信息接口文件: gitee_api_get_commits.yml

name: 获取某个分支的所以提交信息

base_url: https://gitee.com/api/v5

variables:
  token: d66685.......34561e1f5
  owner: youling_zhulongcao
  repo: Test
  branch: master

request:
  url: /repos/$owner/$repo/commits
  method: GET
  params:
    access_token: $token
    sha: $branch

extract:
  - message: content.0.commit.author.name

validate:
  - eq: [status_code, 200]
  - eq: [$message, 悠灵]

 

 

 

2. 完成测试用例文件

  • 获取单个分支测试用例,调用上面对应的接口文件
  • 获取某个分支所有提交信息测试用例,调用上面对应的接口文件

 

获取单个分支测试用例: gitee_test01_get_master.yml

- config:
    name: 测试获取“仓库某个分支”接口

- test:
    name: 获取 master 分支
    api: gitee_api_get_branch.yml

 

 

 

获取某个分支所有提交信息测试用例: gitee_test02_get_commits.yml

- config:
    name: 测试获取“仓库某个分支所有提交”接口

- test:
    name: 测试获取 master 分支所有提交
    api: gitee_api_get_commits.yml

 

 

3. 测试用例集合文件编写规范

 

测试集合的定义规范有两个版本,可以参考源码,这里我们使用 v1 版本规范:

  • httprunner-2.5.5\httprunner\loader\schemas\testsuite.schema.v1.json
  • httprunner-2.5.5\httprunner\loader\schemas\testsuite.schema.v2.json

 

 

testsuite.schema.v1.json 源码,规范测试集合的整体结构:

  • config: 测试集合文件配置区域
  • testcases: 测试用例文件引入区域
  • “.*”: 每个引入的测试用例都需要一个任意内容的描述信息

 

    "properties": {
        "config": {
            "$ref": "common.schema.json#/definitions/config"
        },
        "testcases": {
            "description": "testcase of a testsuite",
            "type": "object",
            "minProperties": 1,
            "patternProperties": {
                ".*": {
                    "description": "testcase definition",
                    "$ref": "testsuite.schema.v1.json#/definitions/testcase"
                }
            }
        }
    },
    "required": [
        "config",
        "testcases"
    ],

 

 

testsuite.schema.v1.json 源码,引用测试用例的规范:

  • name:为引入的用例起个名字
  • variables: 变量的定义在这儿搞定
  • parameters:用例的参数化处理在这儿实现
  • testcase:引入的用例文件名称
"testcase": {
    "type": "object",
    "properties": {
        "name": {
            "$ref": "common.schema.json#/definitions/name"
        },
        "variables": {
            "$ref": "common.schema.json#/definitions/variables"
        },
        "parameters": {
            "description": "generate cartesian product variables with parameters, each group of variables will be run once",
            "type": "object"
        },
        "testcase": {
            "description": "testcase reference, value is testcase file relative path",
            "type": "string"
        }
    },
    "required": [
        "testcase"
    ]
}

 

 

 

按以上规则,编写测试集合文件基本格式如下:

config:
  name: <测试集合名称>

testcases:
  <针对下面引入用例的描述文字>
    name: <自行命名>
    testcase: <被引入的测试用例>

 

 

4.完成测试用例集合文件

  • 调用单个分支测试用例
  • 调用获取某个分支所有提交信息测试用例

 

gitee_suite.yml

config:
  name: 测试获取 “某个分支” 和 “某个分支所有提交” 接口

testcases:
  testcase 1:
    name: 获取“某个分支”
    testcase: gitee_test01_get_master.yml
  
  testcase 2:
    name: 获取“某个分支所有提交”
    testcase: gitee_test02_get_commits.yml

 

 

执行该测试集合文件:

 

查看测试报告:

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值