当前位置:网站首页>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.smoke
3、在测试执行时,输入
-m
参数进行标记执行提示 :如果在运行过程中出现了如下警告则是未在pytest中设置标记
ytestUnknownMarkWarning: Unknown pytest.mark.smoke
代码实例:
参数也可以这样写:
前置后置
setup / teardown
unittest测试框架中的前后置,在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)
边栏推荐
- Openssl3.0 learning XXI provider encoder
- 剑指 Offer 42. 连续子数组的最大和
- Is the knowledge of University useless and outdated?
- leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
- Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
- 5g/4g pole gateway_ Smart pole gateway
- VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
- leetcode2312. Selling wood blocks (difficult, weekly race)
- 479. Additive binary tree (interval DP on the tree)
- From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
猜你喜欢
Design and implementation of key value storage engine based on LSM tree
Six lessons to be learned for the successful implementation of edge coding
5g/4g pole gateway_ Smart pole gateway
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
SQLite 3 of embedded database
leetcode2312. 卖木头块(困难,周赛)
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
Selection of field types for creating tables in MySQL database
Discussion on the idea of platform construction
[Floyd] post disaster reconstruction
随机推荐
Is the knowledge of University useless and outdated?
[question] - why is optical flow not good for static scenes
Redis环境搭建和使用的方法
The concept, function, characteristics, creation and deletion of MySQL constraints
TSINGSEE青犀平台如何实现同一节点同时播放多个视频?
MySQL约束与多表查询实例分析
Four basic strategies for migrating cloud computing workloads
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
1218 square or round
Regular expression learning notes
leetcode2312. 卖木头块(困难,周赛)
Redis有序集合如何使用
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
Three core problems of concurrent programming
Should enterprises choose server free computing?
JMeter (II) - install the custom thread groups plug-in
Niuke - Huawei question bank (51~60)
k线图形态这样记(口诀篇)
电子协会 C语言 1级 32、计算2的幂
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage