超详细从入门到精通,pytest自动化测试框架实战-测试运行钩子(七)


前言

pytest中用例执行相关的钩子函数,所有与运行测试相关的钩子都会接收一个pytest.Item对象。

Pytest自动化测试框架:https://www.bilibili.com/video/BV18K411m7FH/

1、pytest_runtestloop

收集完成后执行主运行测试循环的钩子函数。
参数
session:pytest 会话对象

触发时机:用例收集完后执行

默认钩子实现对会话 ( ) 中收集的所有项目执行 runtest 协议session.items,除非收集失败或collectonly设置了 pytest 选项。

如果在任何时候pytest.exit()调用,循环将立即终止。如果在任何点session.shouldfail或session.shouldstop设置,循环在当前项目的运行测试协议完成后终止。

2、pytest_runtest_protocol

这个钩子函数是用来执行单个用例的,对单个测试项执行
runtest 协议

参数
Item:执行的用例
nextitem: 指定的下一条执行的测试用例

pytest默认的runtest协议为如下三个阶段:
设置阶段:
这个阶段主要执行用例:前置夹具

call = pytest_runtest_setup(item)
report = pytest_runtest_makereport(item, call)
pytest_runtest_logreport(report)
pytest_exception_interact(call, report)

调用阶段
这个阶段负责执行测试用例

call = pytest_runtest_call(item)
report = pytest_runtest_makereport(item, call)
pytest_runtest_logreport(report)
pytest_exception_interact(call, report)

拆解阶段
这个阶段主要执行用例:后置夹具

call = pytest_runtest_teardown(item, nextitem)
report = pytest_runtest_makereport(item, call)
pytest_runtest_logreport(report)
pytest_exception_interact(call, report)

3、pytest_runtest_logstart

在单个项目运行
runtest 协议开始时调用

参数
nodeid : 完整的节点ID
location :包含如下三个值的元组(filename, lineno, testname) ,分别为文件名、行号、用例名称

4、 pytest_runtest_logfinish

在单个项目运行
runtest 协议结束时调用

参数
nodeid : 完整的节点ID
location :包含如下三个值的元组(filename, lineno, testname) ,分别为文件名、行号、用例名称

5、pytest_runtest_setup

在运行runTest协议时
设置阶段执行的钩子函数。该钩子函数默认实现的行为是负责执行前置的测试夹具,以及获取前置夹具中yeild返回的数据。

参数
Item:执行的用例

6、pytest_runtest_call

在运行runTest协议时,调用阶段执行的钩子函数,该钩子函数的默认实现的行为是执行:item.runtest()

参数
Item:执行的用例

7、pytest_runtest_teardown

在运行runTest协议时
拆卸阶段 执行的钩子函数。该钩子函数默认实现的行为是负责执行后置的测试夹具。

参数
Item:执行的用例
nextitem: 执行的下一条用例。

8、pytest_runtest_makereport

该钩子函数,在用例执行
runTest协议的过程中,每个阶段都会调用一次。期作用是为了创建测试执行记录器,记录每个阶段执行的结果。

参数
Item:执行的用例
call: 用例执行的阶段。

9、pytest_pyfunc_call

该钩子函数的作用是为了调用底层的测试执行函数。

参数
pyfuncitem: 最终执行的用例函数

下面是我整理的2023年最全的软件测试工程师学习知识架构体系图

一、Python编程入门到精通

请添加图片描述

二、接口自动化项目实战

请添加图片描述

三、Web自动化项目实战

请添加图片描述

四、App自动化项目实战

请添加图片描述

五、一线大厂简历

请添加图片描述

六、测试开发DevOps体系

请添加图片描述

七、常用自动化测试工具

请添加图片描述

八、JMeter性能测试

请添加图片描述

九、总结(尾部小惊喜)

只有在经历挫折和迷茫的过程中,才能更加坚定地走向成功。让我们不畏困难,勇往直前,为了梦想不断奋斗!

只有奋斗,才会有收获;只有拼搏,才可实现梦想。无论前路多么坎坷,我们都要勇往直前,永不放弃。唯有不断超越自己,才能成就辉煌的人生。

不管你身处何地,不管你面对什么困难,只要你肯努力,就一定能够取得成功。不要放弃你的梦想,勇往直前,付出永远比收获来得晚。

猜你喜欢

转载自blog.csdn.net/m0_70102063/article/details/130043258