当前位置: 首页 > ops >正文

Pytest 插件介绍和开发

插件分类

插件分三种:

1.外部插件(三方开发的)

安装方式:pip install

哪里可以找到三方插件: https://pypi.org/ 

常用插件:

 pytest-ordering

作用:指定执行顺序

使用:(fixture用法类似)@pytest.mark.run(order=1) 

* 尽量不要指定用例顺序,避免发现不到问题

pytest-xdist

作用:可以让用例并行与分布式执行

使用:命令行输入 pytest -n CPU核数 xxx.py或者pytest -n auto

2.本地插件

本地插件就是fixture执行

pytest自动模块发现机制执行fixture(conftest.py存放的)

3.内置插件

内置插件是指勾子函数Hook,pytest执行其实会执行很多函数。具体执行顺序可看:https://ceshiren.com/t/topic/8807

pytest hook 介绍

  • 是个函数,在系统消息触时被系统调用
  • 自动触发机制
  • Hook 函数的名称是确定的
  • pytest有非常多的勾子函数
  • 使用时直接编写函数体

*pytest执行顺序

以下就是为Hook函数,存在 ...\Python38\Lib\site-packages\_pytest\hookspec.py

如果我们需要使用,只需要将这个函数在conftest.py里面重新改写即可

root
└── pytest_cmdline_main
├── pytest_plugin_registered
├── pytest_configure
│ └── pytest_plugin_registered
├── pytest_sessionstart
│ ├── pytest_plugin_registered
│ └── pytest_report_header
├── pytest_collection
│ ├── pytest_collectstart
│ ├── pytest_make_collect_report
│ │ ├── pytest_collect_file
│ │ │ └── pytest_pycollect_makemodule
│ │ └── pytest_pycollect_makeitem
│ │ └── pytest_generate_tests
│ │ └── pytest_make_parametrize_id
│ ├── pytest_collectreport
│ ├── pytest_itemcollected
│ ├── pytest_collection_modifyitems
│ └── pytest_collection_finish
│ └── pytest_report_collectionfinish
├── pytest_runtestloop
│ └── pytest_runtest_protocol
│ ├── pytest_runtest_logstart
│ ├── pytest_runtest_setup
│ │ └── pytest_fixture_setup
│ ├── pytest_runtest_makereport
│ ├── pytest_runtest_logreport
│ │ └── pytest_report_teststatus
│ ├── pytest_runtest_call
│ │ └── pytest_pyfunc_call
│ ├── pytest_runtest_teardown
│ │ └── pytest_fixture_post_finalizer
│ └── pytest_runtest_logfinish
├── pytest_sessionfinish
│ └── pytest_terminal_summary
└── pytest_unconfigure

以下为例子:

Pytest 编写插件 - 添加命令行参数实现切换测试环境和生产环境

重写pytest_addoption hook方法

自定义命令行参数,下面自定义了一个组为manshuo , --env的命令

conftest.py

def pytest_addoption(parser):mygroup = parser.getgroup("manshuo")  # group 将下面所有的 option都展示在这个group下。mygroup.addoption("--env",  # 注册一个命令行选项default='test',  # 参数的默认值dest='env',  # 存储的变量,为属性命令,可以使用Option对象访问到这个值,暂用不到help='set your run env'  # 帮助提示 参数的描述信息)

编写把命令行输入的参数提取出来并根据参数判断是测试环境还是生产环境的方法

conftest.py

@pytest.fixture(scope='session')
def cmdoption(request):myenv = request.config.getoption("--env") #返回命令行参数--env,取出跟在env后面的参数if myenv == 'test':datapath = "data/test/data.yml"elif myenv == 'dev':datapath = "data/dev/data.yml"with open(datapath) as f:datas = yaml.safr_load(f)return myenv,datas

编写用例并使用这个插件

test_demo.py

def test_option(cmdoption):print(cmdoption)

检验:

命令行输入pytest --env dev test_demo.py::test_option

输出:

test_demo.py::test_option hook setup
dev environment
dev
PASSED========================================= 1 passed in 0.03s =========================================

【保姆级教程】手把手教你 测试开发平台必备之高阶插件定制开发,包教包会!

http://www.xdnf.cn/news/19276.html

相关文章:

  • leetcode 260 只出现一次的数字III
  • COLA:大型语言模型高效微调的革命性框架
  • 免费电脑文件夹加密软件
  • 基于Adaboost集成学习与SHAP可解释性分析的分类预测
  • 【K8s】整体认识K8s之存储--volume
  • 在win服务器部署vue+springboot + Maven前端后端流程详解,含ip端口讲解
  • Transformer架构三大核心:位置编码(PE)、前馈网络(FFN)和多头注意力(MHA)。
  • 学习Python中Selenium模块的基本用法(12:操作Cookie)
  • TFS-2005《A Possibilistic Fuzzy c-Means Clustering Algorithm》
  • 使用 Python 自动化检查矢量面数据的拓扑错误(含导出/删除选项)
  • 算法题(196):最大异或对
  • 特殊符号在Html中的代码及常用标签格式的记录
  • Qt组件布局的经验
  • 线程池、锁策略
  • 机器视觉opencv教程(四):图像颜色识别与颜色替换
  • Linux中的ss命令
  • kotlin - 2个Activity实现平行视图,使用SplitPairFilter
  • 网络流量分析——使用Wireshark进行分析
  • Shell脚本编程——变量用法详解
  • Ruoyi-vue-plus-5.x第二篇MyBatis-Plus数据持久层技术:2.2 分页与性能优化
  • DAY17-新世纪DL(DeepLearning/深度学习)战士:Q(机器学习策略)2
  • AI 应用 图文 解说 (二) -- 百度智能云 ASR LIM TTS 语音AI助手源码
  • 自定义AXI_PWM_v1.0——ZYNQ学习笔记15
  • Spring Task快速上手
  • Maven学习笔记01
  • 【stm32】对射式红外传感器计次以及旋转编码器计次
  • SpringBoot 自研运行时 SQL 调用树,3 分钟定位慢 SQL!
  • 用产品经理的思维,重构AI时代的增长Playbook
  • 企业数据湖:从混沌到秩序的分层设计与治理策略
  • 11.1.5 实现文件删除,共享和共享下载排行榜