zoukankan      html  css  js  c++  java
  • httprunner 使用总结

    HttpRunner 概念

    HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试、性能测试、线上监控、持续集成等多种测试需求。

    相关操作

    1. 参数提取(extract)和参数引用($var)
    # 第一个接口/api/get-token的响应结果为:
    {"success": true, "token":"ZQkYhbaQ6q8UFFNE"}
    # 提取 token,采用 content.token
    'extract':[
        {'token':'content.token'}
    ]
    # token 作为提取后的参数名称,可以在后续使用 $token 进行引用
    'headers':{
        'token':'$token',
        "Content-Type": "application/json",
        "device_sn": "FwgRiO7CNA50DSU",
    }
    
    1. 公共配置全局化
    {
      "config": {
        "name": "testcase description", 
        "variables": [],
        "request": {
            "base_url":"http://127.0.0.1:5000",
            "headers": {
                 "User-Agent":"python-requests/2.18.4",
                "device_sn": "FwgRiO7CNA50DSU",
                "Content-Type":"application/json"
            }
        }
      }
    }
    
    # 其中 name 为测试用例的名称,在测试报告中将作为标题
    
    1. debugtalk.py 文件中定义相关变量和函数
    import hashlib
    import hmac
    import random
    import string
    
    SECRET_KEY = "DebugTalk"
    
    def gen_random_string(str_len):
        random_char_list = []
        for _ in range(str_len):
            random_char = random.choice(string.ascii_letters + string.digits)
            random_char_list.append(random_char)
    
        random_string = ''.join(random_char_list)
        return random_string
    
    def get_sign(*args):
        content = ''.join(args).encode('ascii')
        sign_key = SECRET_KEY.encode('ascii')
        sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest()
        return sign
    
    1. 变量的申明(variables)、引用($var)和调用函数(${func($var)})
    # 申明变量:生成 15 位长度的随机字符串并赋值给 device_sn 的代码为:
    
    
    "variables": [
      {"device_sn": "${gen_random_string(15)}"}
    ]
    # 引用变量:使用 $user_agent、$device_sn、$os_platform、$app_version 根据签名算法生成 sign 值的代码
    "json": {
      "sign": "${get_sign($user_agent, $device_sn, $os_platform, $app_version)}"
    }
    
    
    1. 数据驱动
    # 创建用户的接口中对 user_id 进行参数化,参数化列表为 1001~1004,并且取值方式为顺序取值
    "config": {
      "parameters": [
        {"user_id": [1001, 1002, 1003, 1004]}
      ]
    }
    
    1. 测试运行
    # 运行单个测试用例( hrun 命令外加单个测试用例文件的路径)
    
    $ hrun filepath/testcase.yml
    
    # 运行多个测试用例使用 (hrun 命令外加多个测试用例文件的路径)
    
    $ hrun filepath1/testcase1.yml filepath2/testcase2.yml
    
    # 运行指定文件夹下所有的测试用例(使用 hrun 命令外加文件夹的路径):
    
    $ hrun testcases_folder_path
    
    # 测试用例在运行过程中,遇到失败时不再继续运行后续用例
    
    $ hrun filepath/testcase.yml --failfast
    
    # 显示指定日志级别以上的日志
    
    $ hrun tests/data/demo_parameters.yml --log-level debug
    
    1. 测试报告生成
    # 指定报告名称
    $ hrun docs/data/demo-quickstart-2.yml --html-report-name demo
    
    1. hook 机制,hook 函数定义在 debugtalk.py 里
      • 用例层,config 新增关键字 setup_hooks 和 teardown_hooks,为全局的,setup_hooks 在所有测试前执行,teardown_hooks 所有用例执行后执行一次。
      - config:
      name: basic test with httpbin
      request:
          base_url: http://127.0.0.1:3458/
      setup_hooks:
          - ${hook_print(setup)}
      teardown_hooks:
          - ${hook_print(teardown)}
      
      • 测试步骤层,test 中新增关键字 setup_hooks 和 teardown_hooks,为局部变量,当前测试用例测试前后执行
    "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)}"
            }
        },
        "validate": [
            {"eq": ["status_code", 200]}
        ],
        "setup_hooks": [
            "${setup_hook_prepare_kwargs($request)}",
            "${setup_hook_httpntlmauth($request)}"
        ],
        "teardown_hooks": [
            "${teardown_hook_sleep_N_secs($response, 2)}"
        ]
    }
    # test 中 name 为测试步骤的名称,在测试报告中将作为测试步骤的名称
    
    1. 参数定义和数据源指定
      1. 参数名称的定义分为两种情况:
        • 独立参数单独进行定义;
        • 多个参数具有关联性的参数需要将其定义在一起,采用短横线(-)进行连接。
      2. 数据源指定支持三种方式:
        • 在 YAML/JSON 中直接指定参数列表
        • 通过内置的 parameterize(可简写为P)函数引用 CSV 文件
        • 调用 debugtalk.py 中自定义的函数生成参数列表
    # user_id 通过引用 csv 文件
    - config:
        name: "demo"
        parameters:
            - user_agent: ["iOS/10.1", "iOS/10.2", "iOS/10.3"]
            - user_id: ${P(user_id.csv)}
            - username-password: ${get_account(10)}
    # 关联性的多个参数
    - config:
        parameters:
            - username-password:
                - ["user1", "111111"]
                - ["user2", "222222"]
                - ["user3", "333333"]
    

    参数定义详见:
    https://cn.httprunner.org/advanced/parameters/

  • 相关阅读:
    mybaits不能出现小于号
    结合rpyc使用python实现动态升级的方法
    secureCRT使用小贴士
    大数据的实时技术
    gnuplot使用
    Python3.4使用MySql
    Linux内存分配----SLAB
    WinInet:HTTPS 请求出现无效的证书颁发机构的处理
    正则表达式学习
    C++中static的全部作用
  • 原文地址:https://www.cnblogs.com/ronky/p/10062504.html
Copyright © 2011-2022 走看看