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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习de测试小白

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值