httprunner 2.x学习5-测试用例集(testsuite)

前言

httprunner 分层主要是分三层:api、testcase、testsuites
前面讲分层的时候讲到api单独封装每个接口,testcase可以有多个测试步骤,调用api层的接口是写测试用例,用例的步骤是有序的。
testsuites 这一层是测试用例的集合,把测试用例放到一个测试套件去执行,用例执行应该是无序的,有依赖的场景在testcase这一层测试用例里面就已经按步骤写好了。

测试用例集(testsuite)

当测试用例数量比较多以后,为了方便管理和实现批量运行,通常需要使用测试用例集来对测试用例进行组织。

在前文的测试用例分层模型中也强调了,测试用例集(testsuite)是测试用例的 无序 集合,集合中的测试用例应该都是相互独立,不存在先后依赖关系的;如果确实存在先后依赖关系,那就需要在测试用例中完成依赖的处理。

因为是 无序 集合,因此测试用例集的描述形式会与测试用例有些不同,在每个测试用例集文件中,第一层级存在两类字段:

  • config: 测试用例集的总体配置参数
  • testcases: 值为字典结构(无序),key 为测试用例的名称,value 为测试用例的内容;在引用测试用例时也可以指定 variables,实现对引用测试用例中 variables 的覆盖。

非参数化场景

api 层两个接口api/login.yml

# api/login.yml
# 上海-悠悠,httprunner QQ交流群:1121184576
name: login first
base_url: http://127.0.0.1:8000
variables:
    user: test
    password: 123456
request:
        url: /api/v1/login/
        method: POST
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
        json:
            username: $user
            password: $password
validate:
    - eq: [status_code, 200]
    - eq: [headers.Content-Type, application/json]
    - eq: [content.msg, login success!]
    - eq: [content.code, 0]

api/get_userinfo.yml 内容

# api/get_userinfo.yml
# 上海-悠悠,httprunner QQ交流群:1121184576

name: get user info case1
base_url: http://127.0.0.1:8000
variables:
    token: 1c5ef1856edec117ac989eb8def4abbaae28673e
request:
    url: /api/v1/userinfo/
    method: GET
    headers:
        Content-Type: application/json
        User-Agent: python-requests/2.18.4
        Authorization: Token $token          # 引用token

case用例层,写一个登录用例

# case/test_login.yml
# 上海-悠悠,httprunner QQ交流群:1121184576
- config:
    name: "test userinfo"

- test:
    name: login-setup
    api: api/login.yml
    extract:
        - get_token: content.token
    validate:
        - eq: ["status_code", 200]
        - len_eq: ["content.token", 40]

testsuites 层可以把测试用例放一起执行

config:
    name: 创建测试计划
    base_url: http://127.0.0.1:8000

testcases:
    用户test1登陆:
        testcase: case/test_login.yml
        variables:
            user: test1
            password: 123456

    用户test2登陆:
        testcase: case/test_login.yml
        variables:
            user: test2
            password: 123456

注意 testsuites 这一层必须是引用 testcase ,不能直接引用 api

原文地址:https://www.cnblogs.com/yoyoketang/p/13111369.html