07-Httprunner-hook机制

简介: 07-Httprunner-hook机制

前言

  • HttpRunner 从 1.4.5 版本开始实现了全新的 hook 机制,可以在请求前和请求后调用钩子函数。
  • hook 机制分为两个层级:测试用例层面(testcase)和 测试步骤层面(teststep)

关键字

  • 在 YAML/JSON 测试步骤的 test 中新增关键字 setup_hooks 和 teardown_hooks。
  • setup_hooks: 在 HTTP 请求发送前执行 hook 函数,主要用于准备工作;也可以实现对请求的 request 内容进行预处理。
  • teardown_hooks: 在 HTTP 请求发送后执行 hook 函数,主要用于测试后的清理工作;也可以实现对响应的 response 进行修改,例如进行加解密等处理
  • hook 函数的定义放置在项目的 debugtalk.py 中,在 YAML/JSON 中调用 hook 函数仍然是采用 KaTeX parse error: Expected '}', got 'EOF' at end of input: {func(a, $b)} 的形式

编写hook函数

  • 在项目根目录新建debugtalk.py(固定名称,不要自定义)
def hook_print(msg):
    print(msg)

测试用例层面(testcase)

  • httprunner 3.x 版本在 config 中不支持setup_hook和teardown_hook

测试步骤层面(teststep)

  • 传递自定义参数

yaml

config:
    name: "request methods testcase with functions"
    variables:
        foo1: config_bar1
        foo2: config_bar2
        expect_foo1: config_bar1
        expect_foo2: config_bar2
    base_url: "https://postman-echo.com"
    verify: False
    export: ["foo3"]
teststeps:
-
    name: get with params
    variables:
        foo1: bar11
        foo2: bar21
    request:
        method: GET
        url: /get
        params:
            foo1: $foo1
            foo2: $foo2
        headers:
            User-Agent: HttpRunner
    setup_hooks:
        - ${hook_print(setup)}   #  使用hook
    teardown_hooks:
        - ${hook_print(teardown)}
    extract:
        foo3: "body.args.foo2"
    validate:
        - eq: ["status_code", 200]
        - eq: ["content.args.foo1", "bar11"]
        - eq: ["content.args.sum_v", "3"]
        - eq: ["content.args.foo2", "bar21"]
  • $request参数:在测试步骤层面的 setup_hooks 函数中,除了可传入自定义参数外,还可以传入 $request,该参数对应着当前测试步骤 request 的全部内容

示例

def setup_hook_prepare_kwargs(request):
    if request["method"] == "POST":
        content_type = request.get("headers", {}).get("content-type")
        if content_type and "data" in request:
            # if request content-type is application/json, request data should be dumped
            if content_type.startswith("application/json") and isinstance(request["data"], (dict, list)):
                request["data"] = json.dumps(request["data"])
            if isinstance(request["data"], str):
                request["data"] = request["data"].encode('utf-8')
def setup_hook_httpntlmauth(request):
    if "httpntlmauth" in request:
        from requests_ntlm import HttpNtlmAuth
        auth_account = request.pop("httpntlmauth")
        request["auth"] = HttpNtlmAuth(
            auth_account["username"], auth_account["password"])
  • $response参数:在测试步骤层面的 teardown_hooks 函数中,除了可传入自定义参数外,还可以传入 $response,该参数对应着当前请求的响应实例(requests.Response)
def teardown_hook_sleep_N_secs(response, n_secs):
    """ sleep n seconds after request
    """
    if response.status_code == 200:
        time.sleep(0.1)
    else:
        time.sleep(n_secs)


相关文章
|
2月前
|
Android开发
Xposed模块 -- Hook函数参数
Xposed模块 -- Hook函数参数
38 0
|
11月前
|
前端开发 JavaScript
dva的简单使用流程
dva的简单使用流程
70 0
|
2月前
|
关系型数据库 MySQL API
如何使用hook?
如何使用hook?
22 0
|
11月前
|
JavaScript API
自定义hook是什么
自定义hook是什么
|
前端开发
前端hook项目pc总结笔记-hook项目文件上传单独封装请求
前端hook项目pc总结笔记-hook项目文件上传单独封装请求
68 0
|
测试技术 API
【pytest官方文档】解读- 插件开发之hooks 函数(钩子)
【pytest官方文档】解读- 插件开发之hooks 函数(钩子)
【pytest官方文档】解读- 插件开发之hooks 函数(钩子)
ReactHooks11-自定义hook函数
ReactHooks11-自定义hook函数
73 0
ReactHooks11-自定义hook函数
HttpRunner-03-hook
HttpRunner-03-hook
151 0
|
测试技术
Cypress系列(8)- Cypress 编写和组织测试用例篇 之 钩子函数Hook
Cypress系列(8)- Cypress 编写和组织测试用例篇 之 钩子函数Hook
209 0
Cypress系列(8)- Cypress 编写和组织测试用例篇 之 钩子函数Hook