
httprunner3.x hook机制
发布日期:2021-05-13 21:49:03
浏览次数:18
分类:精选文章
本文共 4838 字,大约阅读时间需要 16 分钟。
在接口自动化测试中,处理前置条件和后置条件是非常重要的。为了更好地理解 HttpRunner 框架中的 hook 机制,我将通过一个具体示例来探讨如何在测试步骤中定义和使用 setup_hook 和 teardown_hook,以及如何编写相应的 hook 函数。
假设我们有一个新的测试用例,位于 testcase_student_login_test.py 文件中。每个步骤都将在 teststeps 列表中定义。为了实现前置和后置条件的处理,我将使用 HttpRunner 提供的 hook 机制。
测试步骤的定义
在 Python 文件中,定义一个继承自 HttpRunner 的测试用例类,例如:
from httprunner import (HttpRunner, Config, Step, RunRequest, Parameters)class TestCaseLogin(HttpRunner): @pytest.mark.parametrize('param', Parameters({"device_type": [1]})) def test_start(self, param): super().test_start(param) config = ( Config("login") .base_url("${ENV(HOSTURL)}") .variables({ "username": "${ENV(USERNAME)}", "password": "${ENV(PASSWORD)}", "company_id": "${ENV(COMPANYID)}" }) .verify(False) ) teststeps = [ Step(RunRequest('login') .with_variables({ 'token_length': 32, 'status_code': 201, 'status': 1 }) .setup_hook('${setup_hooks_request($request)}') .post('/client/user/auth') .with_headers({'Content-Type': 'application/json'}) .with_json({ 'scenario': "client", 'company_id': '$company_id', 'user_name': "$username", 'password': "$password", 'device_type': "$device_type", 'device': "" }) .teardown_hook('${teardown_assert_response($response)}') .extract() .with_jmespath("body.data.token", "token") .validate() .assert_equal('status_code', '$status_code', '断言失败') .assert_equal('body.status', '$status', '断言失败') .assert_length_equal('body.data.token', '$token_length', '断言失败') ) ]
定义 setup_hook 和 teardown_hook
我们需要在同一文件中定义 setup_hook 和 teardown_hook。这些函数将在请求发送前和发送后分别执行。
def setup_hooks_request(request): user_agent = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.54' } header = { 'Content-Type': 'application/json', 'User-Agent': user_agent } if 'headers' in request: if bool(request['headers']) is True: request['headers'].update(user_agent) else: request['headers'].update(header) else: request['headers'] = header print('前置条件执行完成')def teardown_assert_response(response): if response.status_code == 200 or 201: if response.body['status'] == 1: print('接口访问成功') else: print('接口请求失败') print('后置条件执行完成')
怎样使用 hook 函数
在测试步骤中,调用 setup_hook 和 teardown_hook 函数:
teststeps = [ Step(RunRequest('login') .with_variables({ 'token_length': 32, 'status_code': 201, 'status': 1 }) .setup_hook('${setup_hooks_request($request)}') .post('/client/user/auth') .with_headers({'Content-Type': 'application/json'}) .with_json({ 'scenario': "client", 'company_id': '$company_id', 'user_name': "$username", 'password': "$password", 'device_type': "$device_type", 'device': "" }) .teardown_hook('${teardown_assert_response($response)}') .extract() .with_jmespath("body.data.token", "token") .validate() .assert_equal('status_code', '$status_code', '断言失败') .assert_equal('body.status', '$status', '断言失败') .assert_length_equal('body.data.token', '$token_length', '断言失败') ]
运行测试
在命令行运行测试用例:
hrun testcase_student_login_test.py -s
运行结果应显示 hook 函数的执行情况:
2021-03-26 18:53:34.028 | INFO | httprunner.runner:test_start:451 - Start to run testcase: login, TestCase ID: de9cc10c-9cde-474e-a86c-2c167d42dafb2021-03-26 18:53:34.030 | INFO | httprunner.runner:__run_step:292 - run step begin: login2021-03-26 18:53:34.031 | INFO | httprunner.runner:__call_hooks:112 - call hook actions: setup request2021-03-26 18:53:34.033 | DEBUG | httprunner.runner:__call_hooks:121 - call hook function: ${setup_hooks_request($request)}前置条件执行完成...2021-03-26 18:53:34.185 | INFO | httprunner.client:request:218 - status_code: 201, response_time(ms): 143.01 ms, response_length: 0 bytes...2021-03-26 18:53:34.186 | INFO | httprunner.runner:__call_hooks:112 - call hook actions: teardown request2021-03-26 18:53:34.188 | DEBUG | httprunner.runner:__call_hooks:121 - call hook function: ${teardown_assert_response($response)}接口访问成功...2021-03-26 18:53:34.192 | INFO | httprunner.response:validate:246 - assert status_code equal 201(int) ==> pass
所有 hook 函数执行正确,测试结果通过。这样,我们实现了前置和后置条件的自动化处理,使每个测试都更加稳健和可靠。
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月17日 08时39分13秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
vcenter访问报503错误处理
2019-03-12
项目计划甘特图绘制说明
2019-03-12
C/C++:线性表之顺序表
2019-03-12
嵌入式系统试题库(CSU)
2019-03-12
图神经网络7日打卡营学习心得
2019-03-12
electronJS 开发linux App
2019-03-12
MbedOS 设备中的模数转换(ADC)
2019-03-12
【vue】setInterval的嵌套实例
2019-03-12
【SpringBoot】如何配置热部署
2019-03-12
【rabbitMQ】04 如何实现高可用?
2019-03-12
【自考】之信息资源管理(一)
2019-03-12
C# 文本框限制大全
2019-03-12
setup facatory9.0打包详细教程(含静默安装和卸载)
2019-03-12
ionic4 路由跳转传值
2019-03-12
CSDN 怎么写出好看的博客
2019-03-12
pwn题shellcode收集
2019-03-12
python中的序列化
2019-03-12
django中使用celery执行异步任务实现
2019-03-12