zoukankan      html  css  js  c++  java
  • httprunner学习9-完整的用例结构(yaml&json)

    前言

    前面几篇零散的学了一些httprunner的知识点,那么一个完整的 YAML/JSON 用例文件包含哪些关键字呢?

    测试用例结构

    在 HttpRunner 中,测试用例组织主要基于三个概念:

    • 测试用例集(testsuite):对应一个文件夹,包含单个或多个测试用例(YAML/JSON)文件
    • 测试用例(testcase):对应一个 YAML/JSON 文件,包含单个或多个测试步骤
    • 测试步骤(teststep):对应 YAML/JSON 文件中的一个 test,描述单次接口测试的全部内容,包括发起接口请求、解析响应结果、校验结果等

    对于单个 YAML/JSON 文件来说,数据存储结构为 list of dict 的形式,其中可能包含一个全局配置项(config)和若干个测试步骤(test)。

    • config:作为整个测试用例的全局配置项
    • test:对应单个测试步骤(teststep),测试用例存在顺序关系,运行时将从前往后依次运行各个测试步骤

    对应的 JSON 格式如下所示:

    [
      {
        "config": {...}
      },
      {
        "test": {...}
      },
      {
        "test": {...}
      }
    ]
    

    对应的 YAML 格式如下所示

    - config:
        name: xxx
    
    - test:
        name: login case1
        request:
            url: http://127.0.0.1:8000/api/v1/login/
        ...
        
    - test:
        name: login case2
        request:
            url: http://127.0.0.1:8000/api/v1/login/
        ...
    

    变量空间(context)作用域

    在测试用例内部,HttpRunner 划分了两层变量空间作用域(context)。

    • config:作为整个测试用例的全局配置项,作用域为整个测试用例;
    • test:测试步骤的变量空间(context)会继承或覆盖 config 中定义的内容;
      • 若某变量在 config 中定义了,在某 test 中没有定义,则该 test 会继承该变量
      • 若某变量在 config 和某 test 中都定义了,则该 test 中使用自己定义的变量值
    • 各个测试步骤(test)的变量空间相互独立,互不影响;
    • 如需在多个测试步骤(test)中传递参数值,则需要使用 extract 关键字,并且只能从前往后传递

    config配置

    关键字 是否必须 格式类型 描述
    name Yes string 测试用例的名称,在测试报告中将作为标题
    variables No list of dict 定义的全局变量,作用域为整个用例
    parameters No list of dict 全局参数,用于实现数据化驱动,作用域为整个用例
    request No dict request 的公共参数,作用域为整个用例;常用参数包括 base_url 和 headers

    request相关参数

    关键字 是否必须 格式类型 描述
    base_url No string 测试用例请求 URL 的公共 host,指定该参数后,test 中的 url 可以只描述 path 部分
    headers No dict request 中 headers 的公共参数,作用域为整个用例
    output No list 整个用例输出的参数列表,可输出的参数包括公共的 variable 和 extract 的参数; 在 log-level 为 debug 模式下,会在 terminal 中打印出参数内容

    config配置 JSON 格式示例

    "config": {
        "name": "testcase description",
        "parameters": [
            {"user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]},
            {"app_version": "${P(app_version.csv)}"},
            {"os_platform": "${get_os_platform()}"}
        ],
        "variables": [
            {"user_agent": "iOS/10.3"},
            {"device_sn": "${gen_random_string(15)}"},
            {"os_platform": "ios"}
        ],
        "request": {
            "base_url": "http://127.0.0.1:5000",
            "headers": {
                "Content-Type": "application/json",
                "device_sn": "$device_sn"
            }
        },
        "output": [
            "token"
        ]
    }
    

    config配置 YAML 格式示例

    - config:
        name: xxx
        parameters:
            - user_agent: ["iOS/10.1", "iOS/10.2", "iOS/10.3"]
            - app_version: ${P(app_version.csv)}
            - os_platform: ${get_os_platform()}
        variables:
            - user_agent: iOS/10.3
            - device_sn: ${gen_random_string(15)}
            - os_platform: ios
        request:
            base_url: http://127.0.0.1:5000
            headers:
                Content-Type: application/json
                device_sn: $device_sn
        output:
            - token
    

    test相关参数

    关键字 是否必须 格式类型 描述
    namel Yes string 测试步骤的名称,在测试报告中将作为测试步骤的名称
    request Yes dict HTTP 请求的详细内容;可用参数详见 python-requests 官方文档
    variables No list of dict 测试步骤中定义的变量,作用域为当前测试步骤
    extract No list 从当前 HTTP 请求的响应结果中提取参数,并保存到参数变量中(例如token),后续测试用例可通过$token的形式进行引用
    validate No list 测试用例中定义的结果校验项,作用域为当前测试用例,用于实现对当前测试用例运行结果的校验
    setup_hooks No list 在 HTTP 请求发送前执行 hook 函数,主要用于准备工作
    teardown_hooks No list 在 HTTP 请求发送后执行 hook 函数,主要用户测试后的清理工作

    test用例 JSON 格式示例

    "test": {
        "name": "get token with $user_agent, $os_platform, $app_version",
        "request": {
            "url": "/api/get-token",
            "method": "POST",
            "headers": {
                "app_version": "$app_version",
                "os_platform": "$os_platform",
                "user_agent": "$user_agent"
            },
            "json": {
                "sign": "${get_sign($user_agent, $device_sn, $os_platform, $app_version)}"
            },
            "extract": [
                {"token": "content.token"}
            ],
            "validate": [
                {"eq": ["status_code", 200]},
                {"eq": ["headers.Content-Type", "application/json"]},
                {"eq": ["content.success", true]}
            ],
            "setup_hooks": [],
            "teardown_hooks": []
        }
    }
    

    test用例 YAML 格式示例

    - test:
        name: get token with $user_agent, $os_platform, $app_version
        request:
            url: /api/get-token
            method: POST
            headers:
                app_version: $app_version
                os_platform: $os_platform
                user_agent: $user_agent
            json:
                sign: ${get_sign($user_agent, $device_sn, $os_platform, $app_version)}
            extract:
                - token: content.token
            validate:
                - eq: [status_code, 200]
                - eq: [headers.Content-Type, application/json]
                - eq: [content.success, true]
            setup_hooks: []
            teardown_hooks: []
    

    hooks

    setup_hooks 函数放置于 debugtalk.py 中,并且必须包含三个参数:

    • method: 请求方法,e.g. GET, POST, PUT
    • url: 请求 URL
    • kwargs: request 的参数字典

    teardown_hooks 函数放置于 debugtalk.py 中,并且必须包含一个参数:

    • resp_obj: requests.Response 实例
  • 相关阅读:
    Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误[2]
    Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误[1]
    进程和线程
    Linux 技巧:让进程在后台可靠运行的几种方法
    Linux 网络命令必知必会之 tcpdump,一份完整的抓包指南请查收!
    这些好用的 Chrome 插件,提升你的工作效率
    golang学习笔记-go mod的使用
    性能监控和分析工具---nmon
    Jenkins
    程序员画图工具总结
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/11575646.html
Copyright © 2011-2022 走看看