文章目录

  • Hook函数的定义
  • pytest的Hook函数,修改pytest-html报告
  • 装饰器pytest.hookimpl(hookwrapper=True)
  • Hook函数排序/调用示例

Hook函数的定义

Hook函数又称为钩子函数,它的作用可以理解成钩住自己喜欢的东西(在window中,喜欢的东西可理解为消息),然后对自己喜欢的东西单独做处理

如:我写了一个window程序,在程序中我写了一段代码(调用window的api来实现钩子),这段代码被系统通过系统调用,把其挂入系统中,然后我就可以对我感兴趣的消息进行处理

我写的这段代码包含有一个回调函数,当有我喜欢的消息发出时,这个回调函数就会执行,所以说,钩子就是指的回调函数

pytest的Hook函数,修改pytest-html报告

可以修改自定义修改pytest-html报告,修改方法如下:
(1)在项目根目录添加confest.py

(2)在confest.py通过为标题行实现自定义钩子函数来修改列,下面的示例在conftest.py脚本中使用测试函数docstring添加描述(Description)列,添加可排序时间(Time)列,并删除链接(Link)列:

from datetime import datetime
from py.xml import html
import pytest@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):cells.insert(2, html.th('Description'))cells.insert(1, html.th('Time', class_='sortable time', col='time'))cells.pop()  #删除最后links列的内容@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):cells.insert(2, html.td(report.description))cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))cells.pop()  #删除最后links列的内容@pytest.mark.hookwrapper   #也可以用@pytest.hookimpl(hookwrapper=True) 两者作用相同
def pytest_runtest_makereport(item, call):  #此钩子函数在setup(初始化的操作),call(测试用例执行时),teardown(测试用例执行完毕后的处理)都会执行一次outcome = yieldreport = outcome.get_result()report.description = str(item.function.__doc__)

参考链接:pytest文档20-pytest-html报告优化(添加Description)
https://www.cnblogs.com/yoyoketang/p/9748718.html

装饰器pytest.hookimpl(hookwrapper=True)

上面提到一个装饰器@pytest.hookimpl(hookwrapper=True),它的作用和装饰器@pytest.mark.hookwrapper是一样的,当pytest调用钩子函数时,它首先执行钩子函数装饰器并传递与常规钩子函数相同的参数(个人理解是当用该装饰器@pytest.hookimpl(hookwrapper=True)装饰时,他会把其他常规钩子函数的参数都传递给当前被装饰的钩子函数)

在钩子函数装饰器的yield处,Pytest将执行下一个钩子函数实现,并以Result对象的形式,封装结果或异常信息的实例的形式将其结果返回到yield处。因此,yield处通常本身不会抛出异常(除非存在错误)

总结如下:
@pytest.hookimpl(hookwrapper=True)装饰的钩子函数,有以下两个作用:
(1)可以获取到测试用例不同执行阶段的结果(setup,call,teardown)
(2)可以获取钩子方法的调用结果(yield返回一个result对象)和调用结果的测试报告(返回一个report对象)

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):   #对于给定的测试用例(item)和调用步骤(call),返回一个测试报告对象(_pytest.runner.TestReport)"""每个测试用例执行后,制作测试报告:param item:测试用例对象:param call:测试用例的测试步骤执行完常规钩子函数返回的report报告有个属性叫report.when先执行when=’setup’ 返回setup 的执行结果然后执行when=’call’ 返回call 的执行结果最后执行when=’teardown’返回teardown 的执行结果:return:"""# 获取常规钩子方法的调用结果,返回一个result对象 out = yield # # 获取调用结果的测试报告,返回一个report对象, report对象的属性包括when(steup, call, teardown三个值)、nodeid(测试用例的名字)、outcome(用例的执行结果,passed,failed)report = out.get_result() print(out) print(report)print(report.when)print(report.nodeid)print(report.outcome)

运行结果:执行三次的原因是此钩子函数会在测试用例执行的不同阶段(setup, call, teardown)都会调用一次

testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest <pluggy.callers._Result object at 0x0000000004B7D828>
<TestReport 'testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest' when='setup' outcome='passed'>
setup
testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest
passed
<pluggy.callers._Result object at 0x0000000002FAA748>
<TestReport 'testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest' when='call' outcome='passed'>
call
testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest
passed
PASSED<pluggy.callers._Result object at 0x000000000303F5F8>
<TestReport 'testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest' when='teardown' outcome='passed'>
teardown
testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest
passed

参考链接
pytest之插件pytest.hookimpl用法https://www.cnblogs.com/vevian/articles/12631555.html
Hook 方法之 pytest_runtest_makereport:获取测试用例执行结果
https://blog.csdn.net/waitan2018/article/details/104347519

Hook函数排序/调用示例

对于任何给定的钩子函数规格,可能存在多个实现,因此我们通常将钩子函数执行视为1:N的函数调用,其中N是已注册函数的数量。有一些方法可以影响钩子函数实现是在其他之前还是之后,即在N-sized函数列表中的位置:

# Plugin 1
@pytest.hookimpl(tryfirst=True)
def pytest_collection_modifyitems(items):# will execute as early as possible...# Plugin 2
@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(items):# will execute as late as possible...# Plugin 3
@pytest.hookimpl(hookwrapper=True)
def pytest_collection_modifyitems(items):# will execute even before the tryfirst one above!outcome = yield# will execute after all non-hookwrappers executed

这是执行的顺序:

  1. Plugin3的pytest_collection_modifyitems被调用直到注入点,因为它是一个钩子函数装饰器。
  2. 调用Plugin1的pytest_collection_modifyitems是因为它标有tryfirst=True。
  3. 调用Plugin2的pytest_collection_modifyitems因为它被标记trylast=True(但即使没有这个标记,它也会在Plugin1之后出现)。
  4. 插件3的pytest_collection_modifyitems然后在注入点之后执行代码。yield接收一个Result实例,该实例封装了调用非装饰器的结果。包装不得修改结果。

以上使用tryfirst,trylast,以及结合hookwrapper=True的示例,它会影响彼此之间hookwrappers的排序

参考链接
Pytest权威教程19-编写钩子(Hooks)方法函数
https://www.cnblogs.com/superhin/p/11478007.html

pytest-html官方文档地址
https://pypi.org/project/pytest-html/

其余参考链接
Pytest官方教程-20-编写钩子(hook)方法
https://www.jianshu.com/p/b8178b693f87
pytest进阶之html测试报告
https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-report.html

pytest文档20-pytest-html报告优化(添加Description)
https://www.cnblogs.com/yoyoketang/p/9748718.html

【pytest】pytest的Hook函数详解相关推荐

  1. linux内核中的hook函数详解,linux内核中的hook函数详解

    在编写linux内核中的网络模块时,用到了钩子函数也就是hook函数.现在来看看linux是如何实现hook函数的. 先介绍一个结构体: struct nf_hook_ops,这个结构体是实现钩子函数 ...

  2. linux hook 任意内核函数,linux内核中的hook函数详解

    在编写linux内核中的网络模块时,用到了钩子函数也就是hook函数.现在来看看linux是如何实现hook函数的. 先介绍一个结构体: struct nf_hook_ops,这个结构体是实现钩子函数 ...

  3. timm 视觉库中的 create_model 函数详解

    timm 视觉库中的 create_model 函数详解 最近一年 Vision Transformer 及其相关改进的工作层出不穷,在他们开源的代码中,大部分都用到了这样一个库:timm.各位炼丹师 ...

  4. 【转】angularjs指令中的compile与link函数详解

    这篇文章主要介绍了angularjs指令中的compile与link函数详解,本文同时诉大家complie,pre-link,post-link的用法与区别等内容,需要的朋友可以参考下 通常大家在使用 ...

  5. pytorch函数详解

    pytorch函数详解 在typora这里写之后复制到简书上 1. torchvision 1.1 transforms.Compose(transforms) 把几个转换组合 example: fr ...

  6. C语言网络编程:accept函数详解

    文章目录 前言 函数描述 代码实例 如何得到客户端的IP 和 端口号 前言 当使用tcp服务器使用socket创建通信文件描述符,bind绑定了文件描述符,服务器ip和端口号,listen将服务器端的 ...

  7. 【FFmpeg】函数详解(三)

    FFmpeg函数详解 14.av_write_frame 15.av_interleaved_write_frame 16.av_write_trailer 17.avio_close 18.av_i ...

  8. 【FFmpeg】函数详解(二)

    FFmpeg函数详解 9.av_dump_format 10.avio_open 11.avformat_write_header 12.avcodec_send_frame 13.avcodec_r ...

  9. 【FFmpeg】函数详解(一)

    FFmpeg函数详解 一.错误码相关 1.AVERROR 2.av_strerror 3.其他错误码解释 二.编解码 1.获取编解码器 2.申请.释放上下文环境 3.打开编码器avcodec_open ...

最新文章

  1. GitHub标星2000+,如何用30天啃完TensorFlow2.0?
  2. tcp/ip协议中消息传输对帧消息的操作
  3. xcode 允许SVN管理项目文件
  4. 收藏:视频网站(JavaEE+FFmpeg)/Nginx+ffmpeg实现流媒体直播点播系统
  5. chrome 70 android,Android版Chrome Beta 70 (70.0.3538.17) 已发布
  6. Forrester:建立企业数据库安全计划
  7. .NET 6新特性试用 | 隐式using指令
  8. 学习OpenVINO笔记之Inference Engine Device Query API
  9. 需求跟踪矩阵模板_需求可追溯性矩阵(RTM)生成程序使用说明
  10. RedHat 9 Linux SendMail 的配置
  11. 软件测试简历职业规划怎么写,职业规划测试
  12. dell服务器开机进系统黑屏,dell电脑开机黑屏,教你几步解决_故障
  13. matlab实现螺旋谱分解,MATLAB实现EMD分解及希尔伯特谱分析
  14. 2022依旧可用的抖音无水印解析工具,免费分享
  15. linux驱动开发 ST7789 LCD驱动移植(I.MX6ULL平台)
  16. 用Python绘制折线图(下)
  17. Es6模板字符串条件判断
  18. 台式计算机主机内置喇叭不响,台式机扬声器没声音怎么样解决
  19. UE4 C++ UMG框架搭建
  20. 《软技能-代码之外的生存能力》第四篇——生产力

热门文章

  1. 天津人民优步司机当周奖励政策 (8.3-8.9)
  2. 月球太阳轨迹matlab,地球月球太阳轨迹 地球和月球运行轨迹图
  3. Altera Scatter-Gather DMA (SG-DMA)的简单使用
  4. 【日语词汇类编】传媒与娱乐:大众传播媒介
  5. 2021-11-02发电机转子方程的推导
  6. Matlab【路径规划】—— 无人机药品配送路线最优化
  7. c语言库函数大全文库,c语言常用的库函数_相关文章专题_写写帮文库
  8. IDEA自动重写的equals方法分析
  9. 台式计算机电源怎么看,怎么看电脑电源多少w 查看自己电脑功耗方法 (全文)
  10. 将yolov5的detect.py改写成可以供其他程序调用的方式,并实现低时延(<0.5s)直播推理