【pytest】Hook 方法之 pytest_collection_modifyitems:修改测试用例执行顺序
pytest_collection_modifyitems 是在用例收集完毕之后被调用,可以用来调整测试用例执行顺序;
它有三个参数,分别是:session:会话对象;
config:配置对象;
items:用例对象列表;
这三个参数分别有不同的作用,都可以拿来单独使用,修改用例执行顺序主要是使用 items 参数!
def pytest_collection_modifyitems(session, config, items):""" called after collection has been performed, may filter or re-orderthe items in-place.:param _pytest.main.Session session: the pytest session object:param _pytest.config.Config config: pytest config object:param List[_pytest.nodes.Item] items: list of item objects"""
修改测试用例执行顺序:
首先准备三个测试用例:
import pytestclass TestDemoA:def test_A_001(self):passdef test_A_002(self):passdef test_A_003(self):passif __name__ == '__main__':pytest.main(['-s'])
普通情况下pytest 会按照从上到下的顺序依次执行(模块级会先以模块名按ascii编码进行排序):
# 控制台输出结果:
============================= test session starts =============================
collected 3 items
test_Z.py::TestDemoA::test_A_001 PASSED
test_Z.py::TestDemoA::test_A_002 PASSED
test_Z.py::TestDemoA::test_A_003 PASSED
============================== 3 passed in 0.03s ==============================
使用 pytest_collection_modifyitems :
在 conftest.py 文件中 使用 pytest_collection_modifyitems 钩子方法:
# conftest.py# 在收集完测试用例后才会执行
def pytest_collection_modifyitems(items):print('pytest 收集到的所有测试用例:\n',items)if __name__ == '__main__':pytest.main(['-s'])
我们可以看到控制台中打印出来了收集到的三个测试用例的对象,而且是在测试用例收集之前便已经执行,在执行完 pytest_collection_modifyitems 之后才显示收集到了 3 个用例,如果我们在 pytest_collection_modifyitems 中对用例进行调整,便会影响用例是否执行和执行顺序;
# 控制台输出结果:
============================= test session starts =============================pytest 收集到的所有测试用例:[<Function test_A_001>, <Function test_A_002>, <Function test_A_003>]collected 3 items
test_Z.py::TestDemoA::test_A_001 PASSED
test_Z.py::TestDemoA::test_A_002 PASSED
test_Z.py::TestDemoA::test_A_003 PASSED============================== 3 passed in 0.02s ==============================
利用 items 获取收集到的用例名和用例节点:
# conftest.pydef pytest_collection_modifyitems(items):print('pytest 收集到的所有测试用例:\n', items)for item in items:print('---' * 10)print('用例名:', item.name)print('用例节点:', item.nodeid)if __name__ == '__main__':pytest.main(['-s'])
# 控制台输出结果:
============================= test session starts =============================pytest 收集到的所有测试用例:[<Function test_A_001>, <Function test_A_002>, <Function test_A_003>]
------------------------------
用例名: test_A_001
用例节点: pytestDemo/pytest_hook_demo/test_Z.py::TestDemoA::test_A_001
------------------------------
用例名: test_A_002
用例节点: pytestDemo/pytest_hook_demo/test_Z.py::TestDemoA::test_A_002
------------------------------
用例名: test_A_003
用例节点: pytestDemo/pytest_hook_demo/test_Z.py::TestDemoA::test_A_003collected 3 items
test_Z.py::TestDemoA::test_A_001 PASSED
test_Z.py::TestDemoA::test_A_002 PASSED
test_Z.py::TestDemoA::test_A_003 PASSED============================== 3 passed in 0.02s ==============================
修改用例执行顺序和剔除测试用例:
# conftest.pydef pytest_collection_modifyitems(items):# 将用例名拿出来存入新列表new_items = []for item in items:new_items.append(item.name)# 1. 删除 test_A_002 用例# 获取 test_A_002 在新列表的索引index_2 = new_items.index('test_A_002')# 在老列表中删除这个索引del items[index_2]del new_items[index_2] # 新列表同步删除,和老列表保持同步# 2. 调换 1 和 3 的顺序# 获取 1 和 3 在新列表的索引index_1 = new_items.index('test_A_001')index_3 = new_items.index('test_A_003')# 根据索引在老列表中调换位置items[index_1], items[index_3] = items[index_3], items[index_1]
可以看到控制台输出的结果中,用例3 和 用例1 的顺序调换了,用例2 被删除没有执行;
代码写的比较粗糙,但是思路就是这样:想办法干涉 items 中用例对象的排序;
# 控制台输出结果:
============================= test session starts =============================
collected 3 items
test_Z.py::TestDemoA::test_A_003 PASSED
test_Z.py::TestDemoA::test_A_001 PASSED============================== 2 passed in 0.02s ==============================
【pytest】Hook 方法之 pytest_collection_modifyitems:修改测试用例执行顺序相关推荐
- Pytest Hooks方法之pytest_collection_modifyitems改变测试用例执行顺序
pytest默认执行用例顺序是根据项目下文件名称按ascii码去收集运行的,文件里的用例是从上往下按顺序执行的. pytest_collection_modifyitems 这个函数顾名思义就是收集测 ...
- 修改QQ执行顺序,获取QQ2008最新版密
作 者: 4st0ne 时 间: 2008-03-20,10:28 链 接: http://bbs.pediy.com/showthread.php?t=61577 本文参考于open[xgc]大侠的 ...
- Pytest Hooks方法之pytest_runtest_makereport获取测试用例结果
pytest提供的钩子(Hooks)方法之pytest_runtest_makereport,可以更清晰的了解用例的执行过程,并获取到每个用例的执行结果. 一.Hook 方法之 pytest_runt ...
- python接口自动化(二十二)--unittest执行顺序隐藏的坑(详解)
简介 大多数的初学者在使用 unittest 框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行.虽然或许通过代码实现了,也是稀里糊涂的一知 ...
- TestNG-详解preserve-order的作用与测试case的执行顺序
在TestNG xml配置文件中,关于<test>的配置里面,有一个属性叫preserve-order,一开始以为这个属性可以用来控制测试case(那些被@Test注解标注的方法)的执行顺 ...
- 从矩阵乘法来看-O优化和ijk执行顺序对程序性能的影响
从矩阵乘法来看-O优化和ijk执行顺序对程序性能的影响 根据计算矩阵乘积的c程序,主要想做想做两件事情: 统计采用不同的优化选项编译程序所用的时间,感受-O优化带来的性能提升. 看看矩阵乘法中不同循环 ...
- 关于线程的执行顺序,可能真的只是你以为的你以为
摘要:今天,我们就一起来看看线程到底是如何执行的,它的顺序又是怎样的? 本文分享自华为云社区<线程的执行顺序与你想的不一样!!>,作者:冰 河 . 一.线程的执行顺序是不确定的 调用Thr ...
- beforeRouteEnter中的this和与created的执行顺序
需求:从page1页进入page2页,page2页需要是一种样式,从其他页面进入page2页需要是另一种样式. 最常见的是从列表页点击详情进入详情页后返回列表页时,列表页需要保持原来的页码,而从其他页 ...
- java静态代码块、静态方法、静态变量、构造代码块、普通代码块、成员变量执行顺序
package smartt.styy.auth.model.voRsp;public class Test {static {String test5="555";//局部变量t ...
最新文章
- 2018-3-7论文网络评论中非结构化信息的表示与研究--------实验分析
- svn idea使用
- 8g ubuntu 树莓派4b_3D 打印制造树莓派 4B 平板电脑
- VS2017登陆不了,TFS无法连接成功的问题
- Serverless,后端小程序的未来
- 洛谷P1157----组合数的输出
- 【acwing210 异或运算】
- 从C# String类理解Unicode(UTF8/UTF16)
- 互联网又一个变态条款 “奋斗者协议”
- linux下ip协议(V4)的实现(二)
- 利用zabbix自动发现监控mongo数据库
- Linux下gdb调试(GUI)
- 非极大值抑制(Non-Maximum Suppression,NMS)
- 磨刀不误砍柴工——利用JMP探索离群值
- catia中的螺旋伞齿轮画法_CATIA齿轮绘制方法
- [VP] 拉普拉斯算子 Laplacian Filter
- Vivado时钟之间的三种关系
- 保龄球计分程序java_保龄球计分——java代码实现
- 使用System.setOut()
- 谷歌Android笔记本,运行安卓+Chrome OS合体新系统:谷歌Pixel 3笔记本被曝光
热门文章
- 使用DataSync迁移数据到Gaussdb T
- SimpleDateFormat线程不安全了?这里有5种解决方案
- 从JAVA内存到垃圾回收,带你深入理解JVM
- ModelArts黑科技揭秘|模型智能评估、诊断,让模型来个“体检
- 前端开发还可以这么玩?元数据实践分享
- 中俄国际社区大佬共话Java,苦Lambda表达式久矣?
- 这种反爬虫手段有点意思,看我破了它!
- 机器学习笔记(二)---- 线性回归
- 开启ntp_飒,NTP时钟服务器在银行大楼系统方案
- href up test.php,test.php