《httprunner 2.x》学习2-手册快速入手

生成测试用例

使用 har2case 转换脚本时默认转换为 JSON 格式,加上 -2y 参数后转换为 YAML 格式。
har2case docs/data/demo-quickstart.har -2y
经过转换,在源 demo-quickstart.har 文件的同级目录下生成了相同文件名称的 YAML 格式测试用例文件 demo-quickstart.yml,其内容如下:
1.每个 YAML/JSON 文件对应一个测试用例(testcase)
2.每个测试用例为一个list of dict结构,其中可能包含全局配置项(config)和若干个测试步骤(test)
3.config 为全局配置项,作用域为整个测试用例
4.test 对应单个测试步骤,作用域仅限于本身

  • config:
    name: testcase description
    variables: {}

  • test:
    name: /api/get-token
    request:
    headers:
    Content-Type: application/json
    User-Agent: python-requests/2.18.4
    app_version: 2.8.6
    device_sn: FwgRiO7CNA50DSU
    os_platform: ios
    json:
    sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
    method: POST
    url: http://127.0.0.1:5000/api/get-token
    validate:
    - eq: [status_code, 200]
    - eq: [headers.Content-Type, application/json]
    - eq: [content.success, true]
    - eq: [content.token, baNLX1zhFYP11Seb]

  • test:
    name: /api/users/1000
    request:
    headers:
    Content-Type: application/json
    User-Agent: python-requests/2.18.4
    device_sn: FwgRiO7CNA50DSU
    token: baNLX1zhFYP11Seb
    json:
    name: user1
    password: '123456'
    method: POST
    url: http://127.0.0.1:5000/api/users/1000
    validate:
    - eq: [status_code, 201]
    - eq: [headers.Content-Type, application/json]
    - eq: [content.success, true]
    - eq: [content.msg, user created successfully.]

调整校验器
例如登录接口的话,每次生成的token不同,因此不要用token做校验项,删除即可。
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]

参数关联
支持参数提取(extract)和参数引用的功能($var)。
先动态获取到第一个测试步骤中的 token,然后在后续测试步骤的请求中使用前面获取到的 token。
extract:
token: content.token

  • test:
    name: /api/users/1000
    request:
    headers:
    Content-Type: application/json
    User-Agent: python-requests/2.18.4
    device_sn: FwgRiO7CNA50DSU
    token: $token

提取base_url
每个测试步骤的 URL 中,都采用的是完整的描述(host+path),但大多数情况下同一个用例中的 host 都是相同的,区别仅在于 path 部分。
因此,我们可以将各个测试步骤(test) URL 的 base_url 抽取出来,放到全局配置模块(config)中,在测试步骤中的 URL 只保留 PATH 部分。

  • config:
    name: testcase description
    base_url: http://127.0.0.1:5000

变量的申明和引用
在 config 和 test 中均可以通过 variables 关键字定义变量,然后在测试步骤中可以通过 $ + 变量名称 的方式引用变量。区别在于,在 config 中定义的变量为全局的,整个测试用例(testcase)的所有地方均可以引用;在 test 中定义的变量作用域仅局限于当前测试步骤(teststep)。

  • test:
    name: /api/get-token
    variables:
    app_version: 2.8.6
    device_sn: FwgRiO7CNA50DSU
    os_platform: ios
    request:
    headers:
    Content-Type: application/json
    User-Agent: python-requests/2.18.4
    app_version: device_sn
    os_platform: $os_platform

抽取公共变量
两个测试步骤中都定义了 device_sn。针对这类公共的参数,我们可以将其统一定义在 config 的 variables 中,在测试步骤中就不用再重复定义。

  • config:
    name: testcase description
    base_url: http://127.0.0.1:5000
    variables:
    device_sn: FwgRiO7CNA50DSU

  • test:
    name: /api/get-token
    variables:
    app_version: 2.8.6
    os_platform: ios
    request:
    headers:
    Content-Type: application/json
    User-Agent: python-requests/2.18.4
    app_version: device_sn

实现动态运算逻辑
参数 device_sn 代表的是设备的 SN 编码,针对 device_sn 生成一个 15 位长度的随机字符串。与此同时,sign 字段是根据 headers 中的各个字段拼接后生成得到的 MD5 值,因此在 device_sn 变动后,sign 也应该重新进行计算,否则就会再次出现签名校验失败的问题。
支持热加载的插件机制(debugtalk.py),可以在 YAML/JSON 中调用 Python 函数。
引用变量的方式仍然与前面讲的一样,采用{func($var)}。
func是函数名,参照httprunner手册:

  • config:
    name: testcase description
    base_url: http://127.0.0.1:5000
    variables:
    device_sn: {get_sign(os_platform, $app_version)}

参数化数据驱动
创建一个文件,对测试用例进行引用,并使用 parameters 关键字定义参数并指定数据源取值方式。
创建用户的接口中对 user_id 进行参数化,参数化列表为 1001~1004,并且取值方式为顺序取值,那么最简单的描述方式就是直接指定参数列表。
config:
name: testcase description

testcases:
create user:
testcase: demo-quickstart-6.yml
parameters:
user_id: [1001, 1002, 1003, 1004]

对于具有关联性的多个参数,例如 username 和 password,那么就可以按照如下方式进行配置:
config:
name: "demo"

testcases:
testcase1_name:
testcase: /path/to/testcase1
parameters:
username-password:
- ["user1", "111111"]
- ["user2", "222222"]
- ["user3", "333333"]

独立参数 & 引用 CSV 文件
对于已有参数列表,并且数据量比较大的情况,比较适合的方式是将参数列表值存储在 CSV 数据文件中。
对于 CSV 数据文件,需要遵循如下几项约定的规则:
CSV 文件中的第一行必须为参数名称,从第二行开始为参数值,每个(组)值占一行;
若同一个 CSV 文件中具有多个参数,则参数名称和数值的间隔符需实用英文逗号;
在 YAML/JSON 文件引用 CSV 文件时,文件路径为基于项目根目录(debugtalk.py 所在路径)的相对路径。

参照链接https://v2.httprunner.org/prepare/parameters/

查看测试报告

在每次使用 hrun 命令运行测试用例后,均会生成一份 HTML 格式的测试报告。报告文件位于 reports 目录下,文件名称为测试用例的开始运行时间。

参考链接
https://v2.httprunner.org/quickstart/#_9

你可能感兴趣的:(《httprunner 2.x》学习2-手册快速入手)