分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

httprunner 使用总结

发布时间:2023-09-06 02:25责任编辑:胡小海关键词:http

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 hashlibimport hmacimport randomimport stringSECRET_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_stringdef 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 httpbinrequest: ???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 为测试步骤的名称,在测试报告中将作为测试步骤的名称
  2. 参数定义和数据源指定
    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/

httprunner 使用总结

原文地址:https://www.cnblogs.com/ronky/p/10062504.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved