当前位置:网站首页>pytest 测试框架
pytest 测试框架
2022-07-02 01:59:00 【佛系的老肖】

简介
pytest 基于unittest封装的第三方测试框架,易学易用,兼容性非常好,支持UI自动化脚本开发,支持接口自动化脚本开发,支持持续集成jenkins,支持分布式执行,支持错误调试,支持错误重跑机制,支持allure报告框架
环境配置(安装)
pip install pytest—脚本开发与执行pip install pytest-html—脚本运行生成测试报告pip install pytest-xdist—脚本运行分布式执行
语法规则
- pytest脚本名称命名:以test开头或test结尾
- pytest脚本中的类名:以Test开头
- pytest脚本中的函数/方法名:以test开头
- pytest脚本的包中必须要有__init__.py
- pytest脚本中的类中不允许含有构造方法__init__
- pytest脚本作用于函数,亦可作用于类和方法
- pytest脚本运行未设置的情况下,安装函数或类及方法的ASCII顺序执行
例子 :

- 导包import pytest
- 定义类/函数test
- 断言:assert
- 执行:pytest.main([‘test005.py’]),执行参数一定是list列表形式
执行方式
pytest.main(['[脚本名称]','-s'])执行当前脚本下的用例,打印printpytest.main(['[脚本名称]::脚本下的类名称::类下面的方法名称','-v'])指定类下的方法执行用例pytest.main(['[脚本名称]::脚本下的类名称', '-v','--html=report.html'])指定测试类执行用例pytest.main([__file__])#指定当前模块执行测试
执行参数
-s:执行print()pytest.main(['[包名]::类名','-s'])-v:详细输出日志pytest.main(['[包名]::类名::方法名称','-v'])-n:指定分布式执行的次数 ‘-n’ ‘2’pytest.main(['[包名]','-n','2'])-q:安静模式,极简模式,不加载环境配置信息及日志信息,尽量与-v不要同时使用pytest.main(['[包名]','-q'])-m:标记执行,支持逻辑运算符-k:标记执行 ,模糊匹配 支持逻辑运算符-x:执行失败一次就终止测试--maxfail=1:设置失败的最大次数--rerun=2:指定次数为两次 需要安装 pytest-rerunfailures
参数化
单个参数
@pytest.mark.parametrize('参数',['参数对应的值'])例子:
@pytest.mark.parametrize('username',['admin'])---单个参数
一组参数
@pytest.mark.parametrize('参数1,参数2',[['参数1对应的值','参数2对应的值']])例子:
@pytest.mark.parametrize('username,password',[['admin','milor123']])一组参数
多组参数
@pytest.mark.parametrize('参数1,参数2',[['参数1对应的值','参数2对应的值'],['参数1对应的值','参数2对应的值']])例子:
@pytest.mark.parametrize('username,password',[['admin','milor123'],['lm','LiuM123']])
跳过执行
- 无条件跳过:@pytest.mark.skip(reason=‘没有理由’)
- 有条件跳过:@pytest.mark.skipif(1<2,reason=‘当1<2时,跳过’)
- 方法或函数内部无条件跳过:pytest.skip(msg=‘skip’)
标记执行
-k模糊匹配标记 标记含有某个字符串的测试用例进行执行操作(支持逻辑运算符 and or )例
pytest.main([__file__,'-s','-k','login']) #执行用例名称中包含login的测试用例 pytest.main([__file__, '-s', '-k', 'login and index ']) #执行用例名称中包含login和index的测试用例 pytest.main([__file__, '-s', '-k', 'login or index ']) #执行用例名称中包含login或index的测试用例-m标记 设置标记,在配置文件pytest.ini 设置markers,支持设置多个标记,应用场景:冒烟测试、回归测试等,支持逻辑运算符 and or1、
pytest.ini放置在pytest 工作目录中,创建这个文件,编辑内容
2、在需要打标记的测试用例上面使用装饰器
@pytest.mark.smoke3、在测试执行时,输入
-m参数进行标记执行提示 :如果在运行过程中出现了如下警告则是未在pytest中设置标记
ytestUnknownMarkWarning: Unknown pytest.mark.smoke代码实例:

参数也可以这样写:


前置后置
setup / teardownunittest测试框架中的前后置,在pytest兼容,function和method 功能类似于构造__init__与析构__del__
每条测试用例执行之前均要执行一次setup
每条测试用例执行完之后均要执行一次teardown
适用于函数或类下的方法—测试用例
setup_function /teardown_function应用于函数中,每个测试函数执行之前进行setup初始化,执行之后进行teardown回收资源,恢复现场
setup_module / teardown_module应用于函数中,针对当前模块,所有测试函数执行之前进行setup初始化,执行之后进行teardown回收资源,恢复现场
setup_class / teardown_class应用于类中,所有测试函数执行之前进行setup初始化,执行之后进行teardown回收资源,恢复现场
setup_method / teardown_methon应用于类中,每个测试方法执行之前进行setup初始化,执行之后进行teardown回收资源,恢复现场
总结:
- 测试场景:每条测试用例执行之前均要做的重复的动作,执行之后均要执行的重复的动作,比如:针对单个模块进行测试需要相同的前置条件时,可以适用此方法。测试登录,登录用例覆盖有10条用例,每次执行前需要打开浏览器,执行后需要关闭浏览器 setup setup_function setup_method

测试场景:所有测试用例执行时均需要某个对象dr或者某种状态session,需要持续使用,可以适用此方法,比如:测试登录之后的模块,需要登录的driver或者会话session setup_module setup_class

固件
语法 :
@pytest.fixture(scope='function',name='myfix',autouse=True)- scope:适用的范围,默认function,支持class,method,module
- autouse:默认值为False,设置为True,会自动执行固件装饰器
- name:别名固件装饰器-
代码:

- 应用场景:与前置后置用法类似,比如:UI自动化测试中的driver与资源回收,接口自动化测试中初始化与资源回收
代码演示:

脚本执行模式
工具中run脚本,走main入口
命令行模式运行pytest脚本名称
pytest [脚本名称.py]::test_login -s -m smoke 指定模块下的测试用例标记执行,并打印print内容
执行顺序
从上往下依次执行,可以使用 order参数进行排序
@pytest.mark.run(order=2)
@pytest.mark.run(order=1)
边栏推荐
- matlab 实现语音信号重采样和归一化,并播放比对效果
- 剑指 Offer 29. 顺时针打印矩阵
- leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
- Matlab uses resample to complete resampling
- MySQL约束与多表查询实例分析
- 2022 Q2 - Summary of skills to improve skills
- 剑指 Offer 62. 圆圈中最后剩下的数字
- mysql列转行函数指的是什么
- 734. Energy stone (greed, backpack)
- What is AQS and its principle
猜你喜欢

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

734. Energy stone (greed, backpack)

遷移雲計算工作負載的四個基本策略

JPM 2021 most popular paper released (with download)

PR second training

From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
![[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology

Learn basic K-line diagram knowledge in three minutes

成功实现边缘编码需要了解的六大经验教训

matlab 使用 resample 完成重采样
随机推荐
OpenCASCADE7.6编译
【毕业季】研究生学长分享怎样让本科更有意义
What is the MySQL column to row function
PR second training
How does MySQL solve the problem of not releasing space after deleting a large amount of data
Construction and maintenance of business websites [14]
如何用一款产品推动「品牌的惊险一跃」?
JMeter (I) - download, installation and plug-in management
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
matlab 使用 audioread 、 sound 读取和播放 wav 文件
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
Ubuntu20.04 PostgreSQL 14 installation configuration record
Makefile simple induction
【深度学习】infomap 人脸聚类 facecluster
How to use redis ordered collection
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
1069. Division of convex polygons (thinking, interval DP)
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
JPM 2021 most popular paper released (with download)
734. Energy stone (greed, backpack)