当前位置:网站首页>自动化测试框架Pytest(一)——入门
自动化测试框架Pytest(一)——入门
2022-08-10 06:49:00 【测试小娜】
今天来学习一款Python的自动化框架——pytest
一、单元测试框架
java:junit和testing
python:unittest和pytest
单元测试框架主要做什么:
1、测试发现:从多个文件里找测试用例
2、测试执行:按照一定的顺序和规则去执行,并生成结果
3、测试判断:通过断言判断预期结果和实际结果的差异
4、测试报告:统计测试进度、耗时、通过率,生成测试报告
pytest简介:
1、pytest是一个非常成熟的Python单元框架,比unittest更灵活,容易上手
2、pytest可以和selenium、requests、appium实现web自动化、接口自动化、app自动化
3、pytest可以实现测试用例的跳过一级reruns失败用例重试
4、pytest可以和allure生成非常美观的测试报告
5、pytest可以和jenkins持续集成
6、pytest有很多强大的插件
二、使用pytest
1、模块名必以test_开头或_test结尾
2、测试类名必须以Test开头,并且不能有init方法
3、测试方法必须以test开头
三、pytest测试用例的运行方式
1、主函数模式
(1)运行所有:
pytest.main()
(2)运行模块:
pytest.main(['-vs','./testcase/test_01.py'])
(3)指定文件夹:
pytest.main(['-vs','./testcase'])
(4)nodeid方式启动:
pytest.main(['-vs','./testcase/test_01.py::TestLogin::test_01'])
2、命令行模式
(1)运行所有:
pytest
(2)运行模块:
pytest -vs ./testcase/test_01.py
(3)指定文件夹:
pytest -vs ./testcase
(4)nodeid方式启动:
pytest -vs ./testcase/test_01.py::TestLogin::test_01
参数:
-s:输出调试信息,包括print打印的信息
-v:显示详细信息
-k:根据测试用例的部分字符串指定测试用例
pytest -k "02"
-n:几个线程并行跑
pytest -vs -n 2
pytest.main(['-vs','-n=2'])
-x:只要有一个用例报错,那么测试就停止
3、通过读取pytest.ini配置文件运行
[pytest]
addopts = -vs
testpaths = ./testcase
python_files = test_02.py
python_classes = Test*
python_functions = test
1、位置一般放在项目的根目录。
2、编码必须是ANSI,可以使用notpad++修改编码格式。
3、作用是改变pytest默认的行为。
4、不管是主函数的模式运行还是命令行的模式运行
四、rerunfailures失败重试
1、pip升级
python -m pip install pip==20.2.4
2、安装 pytest-rerunfailures
python -m pip install pytest-rerunfailures
3、main函数添加参数 --rerun=重试次数
pytest.main(['-vs','--reruns=2'])
4、terminal执行指令:
pytest -vs ./testcase --reruns 2
可以看到执行失败的用例会重新执行2次
五、pytest的执行顺序
通过@pytest.mark.run(order=?)标记来指定顺序,数字越小的越先执行
class TestLogin:
@pytest.mark.run(order=1)
def test_01(self):
print('测试百里守约')
@pytest.mark.run(order=3)
def test_02(self):
print('测试娜可露露')
@pytest.mark.run(order=2)
def test_03(self):
print('测试蔡文姬')
六、分组执行
通过@pytest.mark.XXX来进行分组
@pytest.mark.smoke
def test_01(self):
print('测试蔡文姬')
@pytest.mark.usermanage
def test_02(self):
print('测试东皇')
pytest.ini
[pytest]
addopts = -vs -m "smoke or usermanage"
testpaths = ./testcase
python_files = test_02.py
python_classes = Test*
python_functions = test
markers =
smoke:冒烟用例
usermanage:用户管理模块
也可以terminal执行:
pytest -vs -m "smoke or usermanage"
七、pytest跳过测试用例
1、无条件跳过,可以后面不加reason
@pytest.mark.skip(reason="无敌")
def test_04(self):
print('测试东皇')
2、有条件跳过
age = 15
@pytest.mark.skipif(age<18,reason="未成年")
def test_05(self):
print('测试跳过')
3、terminal执行pytest
八、生成测试报告
addopts = -vs --html ./report/report.html
下一章介绍pytest的初始化和相关工具
学习资源分享
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走
这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….
如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加入下方我的qq群大家一起讨论交流,里面也有各种软件测试资料和技术交流。
边栏推荐
- pytest之parametrize参数化
- Chapter 12 Other Database Tuning Strategies [2. Index and Tuning] [MySQL Advanced]
- 2022河南萌新联赛第(五)场:信息工程大学 K - 矩阵生成
- Unity3d famous project-Dark Tree translation
- 椭圆曲线离散对数问题以及求解
- Sort binary tree code
- MySQL索引事务
- order by injection and limit injection, and wide byte injection
- 裸辞—躺平—刷题—大厂(Android面试的几大技巧)
- 第11章 数据库的设计规范【2.索引及调优篇】【MySQL高级】
猜你喜欢
随机推荐
【无标题】
High quality WordPress download station 5 play theme template
Basic use of Log4j2
英国国家卫生服务遭受攻击,系统出现大面积故障
基于ABP的AppUser对象扩展
复现dns外带数据结合sqlmap
Qt使用私有接口绘制窗口阴影
强化学习_08_Datawhale针对连续动作的深度Q网络
Lunix(阿里云服务器)安装Anaconda并开启jupyter服务本地访问
强化学习_03_表格方法实践(CartPole-v0 And MontoCarlo)
高级测试:如何使用Flink对Strom任务的逻辑功能进行复现测试?
Big guy, when Oracle single-table incremental synchronization, the source database server takes up nearly 2g of memory. This is not normal, right?
第2章 变量和基本类型读书笔记
机器学习_LGB调参汇总(开箱即食)
3-6月面经总结,200多页真题笔记和详解(含核心考点及6家大厂)
2022河南萌新联赛第(五)场:信息工程大学 C - 丢手绢
2022 Henan Mengxin League Game (5): University of Information Engineering F - Split Turf
强化学习_06_pytorch-DQN实践(CartPole-v0)
COLMAP+OpenMVS实现物体三维重建mesh模型
C语言文件操作