【HttpRunner学习总结2】---- hook机制 钩子函数

一、概述

HttpRunner 已实现了全新的 hook 机制,可以在请求前和请求后调用钩子函数。

二、调用 hook 函数

hook 机制分为两个层级:

  • 运行测试用例层面(RunTestCase)
  • 运行请求层面(RunRequest)

1.运行测试用例层面(RunTestCase)

在 pyttest 测试用例的 RunTestCase 中增加关键字 setup_hooksteardown_hooks

  • setup_hooks: 在调用整个用例开始执行前触发 hook 函数,主要用于准备工作。
  • teardown_hooks: 在调用整个用例结束执行后触发 hook 函数,主要用于测试后的清理工作。
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

from WeChat.testcases.get_access_token_test import TestCaseGetAccessToken


class TestCaseSendMessage(HttpRunner):
    config = (
        Config("发送消息")
        .base_url("${ENV(base_url)}")
        .verify(True)
    )

    teststeps = [
        Step(
            RunTestCase("更新token")
            .setup_hook("${hook_print(setup--更新token之前的准备工作)}")
            .call(TestCaseGetAccessToken)
            .teardown_hook("${hook_print(teardown--更新token之后的收尾工作)}")
        ),
        Step(
            RunRequest("发送文本内容的消息")
            .post("/cgi-bin/message/send?access_token=$access_token")
            .with_json(
                {
                    "touser": "@all",
                    "msgtype": "text",
                    "agentid": 1000002,
                    "text": {
                        "content": "你的快递已到,请携带工卡前往邮件中心领取。聪明避开排队。"
                    }
                }
            )
            .validate()
            .assert_equal("body.errcode", 0)
            .assert_equal("body.errmsg", "ok")
        )
    ]

if __name__ == "__main__":
    TestCaseSendMessage().test_start()

2.运行请求层面(RunRequest)

在 pytest 测试用例的 RunRequest 中新增关键字 setup_hooksteardown_hooks

  • setup_hooks: 在 HTTP 请求发送前执行 hook 函数,主要用于准备工作;也可以实现对请求的 request 内容进行预处理。
  • teardown_hooks: 在 HTTP 请求发送后执行 hook 函数,主要用于测试后的清理工作;也可以实现对响应的 response 进行修改,例如进行加解密等处理。
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase


class TestCaseHooks(HttpRunner):

    config = (
        Config("basic test with httpbin")
        .variables(**{"server":"https://httpbin.org"})
        .base_url("$server")
        .verify(True)
    )

    teststeps = [
        Step(
            RunRequest("headers")
            .setup_hook("${setup_hook_add_kwargs($request)}")
            .setup_hook("${setup_hook_remove_kwargs($request)}")
            .setup_hook("${setup_hook_set_cookies_value($request)}")
            .get("/headers")
            .teardown_hook("${teardown_hook_sleep_N_secs($response, 1)}")
            .validate()
            .assert_equal("status_code", 200)
            .assert_contained_by("body.headers.Host", "$server")
        ),
        Step(
            RunRequest("修改 response 对象")
            .get("/headers")
            .teardown_hook("${alter_response($response)}")
            .validate()
            .assert_equal("status_code", 500)
            .assert_equal('headers."Content-Type"', "html/text")
            .assert_equal("body.headers.Host", "127.0.0.1:8888")
        )
    ]


if __name__ == "__main__":
    TestCaseHooks().test_start()

三、编写 hook 函数

hook 函数的定义放置在项目的 debugtalk.py 中,在 pytest/YAML/JSON 中调用 hook 函数仍然是采用 ${func($a, $b)} 的形式。

对于测试用例层面的 hook 函数,与 YAML/JSON 中自定义的函数完全相同,可通过自定义参数传参的形式来实现灵活应用。

from loguru import logger
def hook_print(msg):
    logger.info(msg)

对于单个测试请求层面的 hook 函数,除了可传入自定义参数外,还可以传入与当前测试用例相关的信息,包括请求的 $request 和响应的 $response,用于实现更复杂场景的灵活应用。

1.setup_hooks

在测试步骤层面的 setup_hooks 函数中,除了可传入自定义参数外,还可以传入 $request,该参数对应着当前测试步骤 request 的全部内容。因为 request 是可变参数类型(dict),因此该函数参数为引用传递,当我们需要对请求参数进行预处理时尤其有用。

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"])

通过上述的 setup_hook_prepare_kwargs 函数,可以实现根据请求方法和请求的 Content-Type 来对请求的 data 进行加工处理;通过 setup_hook_httpntlmauth 函数,可以实现 HttpNtlmAuth 权限授权。

2.teardown_hooks

在测试步骤层面的 teardown_hooks 函数中,除了可传入自定义参数外,还可以传入 $response,该参数对应着当前请求的响应实例(requests.Response)。

def teardown_hook_sleep_N_secs(response, n_secs):
    """
    请求后休眠n秒
    """
    if response.status_code == 200:
        time.sleep(0.1)
    else:
        time.sleep(n_secs)

通过上述的 teardown_hook_sleep_N_secs 函数,可以根据接口响应的状态码来进行不同时间的延迟等待。

另外,在 teardown_hooks 函数中还可以对 response 进行修改。当我们需要先对响应内容进行处理(例如加解密、参数运算),再进行参数提取(extract)和校验(validate)时尤其有用。

例如在下面的测试步骤中,在执行测试后,通过 teardown_hooks 函数将响应结果的状态码和 headers 进行了修改,然后再进行了校验。

def alter_response(response):
    response.status_code = 500
    response.headers["Content-Type"] = "html/text"
    response.body["headers"]["Host"] = "127.0.0.1:8888"
    response.new_attribute = "new_attribute_value"
    response.new_attribute_dict = {"key": 123}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值