zoukankan      html  css  js  c++  java
  • 测试框架学习之HttpRunner测试用例准备(三)

    测试用例准备(有以下一些方式录制进行转换,以Charles为例)


    1.1、Charles Proxy工具抓包并导出.har数据包

     

     

    1.2、fiddler工具抓包并导出.har数据包

     

     

     



    2、命令行终端中运行 har2case 命令,即可将 demo.har 转换为 HttpRunner 的测试用例文件(YAML 或 JSON 格式,第二个参数为空,则默认转换为 JSON 格式)
    2.1、har2case 可将 HAR 文件转换为 YAML 或 JSON 格式的测试用例,具体转换为什么形式取决于第二个参数(testcase_path)。
    $ har2case /path/to/demo.har testcase_path
    2.2、若第二个参数为空,则默认转换为 JSON 格式的测试用例,文件名称和路径与 HAR 源文件相同。
    $ har2case docs/data/demo-quickstart.har
    INFO:root:Generate JSON testcase successfully: docs/data/demo-quickstart.json
    2.3、若文件后缀为.yml/.yaml,则转换生成 YAML 格式的测试用例。
    $ har2case docs/data/demo-quickstart.har demo.yml
    INFO:root:Generate YAML testcase successfully: demo.yml
    2.4、若文件后缀为.json,则转换生成 JSON 格式的测试用例。
    $ har2case docs/data/demo-quickstart.har demo.json
    INFO:root:Generate JSON testcase successfully: demo.json

    转换后内容模板如下(需要调整才能运行)

    JSON格式


    [
    {
    "config": {
    "name": "testcase description",
    "variables": [],
    "request": {
    "base_url": "",
    "headers": {
    "User-Agent": "python-requests/2.18.4"
    }
    }
    }
    },
    {
    "test": {
    "name": "/api/get-token",
    "request": {
    "url": "http://127.0.0.1:5000/api/get-token",
    "headers": {
    "device_sn": "FwgRiO7CNA50DSU",
    "user_agent": "iOS/10.3",
    "os_platform": "ios",
    "app_version": "2.8.6",
    "Content-Type": "application/json"
    },
    "method": "POST",
    "json": {"sign": "958a05393efef0ac7c0fb80a7eac45e24fd40c27"}
    },
    "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": {
    "url": "http://127.0.0.1:5000/api/users/1000",
    "headers": {
    "device_sn": "FwgRiO7CNA50DSU",
    "token": "baNLX1zhFYP11Seb",
    "Content-Type": "application/json"
    },
    "method": "POST",
    "json": {"name": "user1", "password": "123456"}
    },
    "validate": [
    {"eq": ["status_code", 201]},
    {"eq": ["headers.Content-Type", "application/json"]},
    {"eq": ["content.success", true]},
    {"eq": ["content.msg", "user created successfully."]}
    ]
    }
    }]

    YAML格式

    - config:
    name: testcase description
    parameters:
    - user_id: [1001, 1002, 1003, 1004]
    variables:
    - device_sn: ${gen_random_string(15)}
    - user_id: 1000
    request:
    base_url: http://127.0.0.1:5000
    headers:
    User-Agent: python-requests/2.18.4
    device_sn: $device_sn
    Content-Type: application/json

    - test:
    name: /api/get-token
    variables:
    - user_agent: "iOS/10.3"
    - os_platform: "ios"
    - app_version: "2.8.6"
    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]

    - test:
    name: /api/users/$user_id
    request:
    url: /api/users/$user_id
    method: POST
    headers:
    token: $token
    json:
    name: user1
    password: '123456'
    validate:
    - eq: [status_code, 201]
    - eq: [headers.Content-Type, application/json]
    - eq: [content.success, true]
    - eq: [content.msg, "user created successfully."]



    用例说明
    1、权限校验,获取 token
    2、支持 CRUD 操作的 RESTful APIs,所有接口的请求头域中都必须包含有效的 token
    3、每个 YAML/JSON 文件对应一个测试用例(testcase)
    4、每个测试用例为一个list of dict结构,其中可能包含全局配置项(config)和若干个测试步骤(test)
    5、config 为全局配置项,作用域为整个测试用例
    6、test 对应单个测试步骤,作用域仅限于本身


    用例准备已OK,接着用例脚本调试!
  • 相关阅读:
    一元多项式的运算
    单链表逆转
    字符串函数
    历届试题 错误票据
    不用循环,不用递归,输出1~1000的整数
    sql三维数据
    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问 (也就是跨数据库访问出错)
    由于服务器意外的断电,导致SQL SERVER服务器上数据库出现“置疑”而无法使用,
    关于delphi7的四舍五入
    关于delphi exit 继承
  • 原文地址:https://www.cnblogs.com/mys6/p/14768459.html
Copyright © 2011-2022 走看看