hook钩子
https://ontheway.cool/httprunner3DocsForCN/concepts/hook/
从 1.4.5 版本开始,全新的 hook 机制,在请求前和请求后调用钩子函数
调用 hook 函数
testcase测试用例的hook
yaml/json测试用例的config中新增关键字setup_hooks和teardown_hooks
setup_hooks测试用例开始前钩子
测试前的准备工作
teardown_hooks测试用例结束后钩子
测试后的清理工作
testcase测试用例的hook demo
- config:
name: basic test with httpbin
base_url: http://127.0.0.1:3458/
# 测试用例开始前钩子
setup_hooks:
- ${xueyu_scrapy_hook(setup)}
# 测试用例结束后钩子
teardown_hooks:
- ${xueyu_scrapy_hook(teardown)}
teststep测试步骤的hook
yaml/json测试步骤的test中新增关键字setup_hooks和teardown_hooks
setup_hooks测试步骤开始前钩子
http请求发送前执行hook函数,测试前的准备工作,修改请求request
teardown_hooks测试步骤结束后钩子
http请求发送后执行hook函数,测试后的清理工作,修改响应的response
teststep测试步骤的hook demo
teststeps:
-
name: request with functions
variables:
foo1: testcase_ref_bar1
expect_foo1: testcase_ref_bar1
testcase: testcases/demo_testcase_request.yml
export:
- foo3
# 测试步骤钩子,测试步骤开始前
setup_hooks: [
"${setup_hook_prepare_kwargs($request)}",
"${setup_hook_httpntlmauth($request)}"
]
# 测试步骤钩子,测试步骤结束后
teardown_hooks: [
"${teardown_hook_sleep_N_secs($response, 2)}"
]
hook函数
项目的 debugtalk.py 中定义hook 函数
# python调用hook函数
RunRequest('请求前,钩子更改with_json请求体')
# 请求前钩子
.setup_hook('${xueyu_scrapy_hook($request)}')
# yaml/json 中调用 hook 函数
${xueyu_scrapy($a, $b)}
对于测试用例层面的 hook 函数,与 yaml/json 中自定义的函数完全相同
通过自定义参数传参的形式来实现灵活应用
# debugtalk.py
import random
# 钩子
def xueyu_scrapy_hook(request):
request['req_json']['button'] = random.choice(request['req_json']['content'])
request['req_json']['content'] = random.choice(request['req_json']['content'])
传入自定义参数,只能修改请求,目前找不到改全局变量的办法
传入请求 $request和响应$response对象,可以修改请求和响应对象
setup_hooks
在测试步骤 setup_hooks 函数,除了传入自定义参数,还可以传入 $request
$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 权限授权。
teardown_hooks
在测试步骤层面的 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)
通过上述的 teardown_hook_sleep_N_secs 函数,可以根据接口响应的状态码来进行不同时间的延迟等待。
另外,在 teardown_hooks 函数中还可以对 response 进行修改。当我们需要先对响应内容进行处理(例如加解密、参数运算),再进行参数提取(extract)和校验(validate)时尤其有用。
例如在下面的测试步骤中,在执行测试后,通过 teardown_hooks 函数将响应结果的状态码和 headers 进行了修改,然后再进行了校验。
- test:
name: alter response
request:
url: /headers
method: GET
teardown_hooks:
- ${alter_response($response)}
validate:
- eq: ["status_code", 500]
- eq: ["headers.content-type", "html/text"]
def alter_response(response):
response.status_code = 500
response.headers["Content-Type"] = "html/text"