当前位置:网站首页>自动化测试框架Pytest(二)——前后置处理
自动化测试框架Pytest(二)——前后置处理
2022-08-10 06:49:00 【测试小娜】
有一些初始化配置和测试之后的收尾,只需要处理一次,这个时候我们就要用到夹具。

一、pytest的前后置(固件、夹具)处理
1、setup/teardown,setup_class/teardown_class
setup/teardown:在每个用例的前后都会执行
setup_class/teardown_class:在每个类的前后都会执行
import pytestclass TestLogin:def setup_class(self):print('------------------setup_class-----------------')def setup(self):print('------------------setup-----------------')def test_01(self):print('测试百里守约')def test_02(self):print('测试娜可露露')def test_03(self):print('测试蔡文姬')def teardown(self):print('--------------------teardown_----------------')def teardown_class(self):print('--------------------teardown_class----------------')

二、使用fixture装饰器来实现部分用例的前后置
@pytest.fixture(scope="",params="",autouse="",ids="",name="")scope:标记的作用域。function(默认)、class、module、packageparams:参数化autouse:True自动执行,默认是Falseids:当使用params参数化时,给每一个值设置一个变量名,意义不大name:给标记的方法取一个别名
1、scope,默认是function
import pytest@pytest.fixture(scope="function")def my_fixture():print('\n前置方法,可以实现部分及全部用例的前置')yieldprint('\n后置方法,可以实现部分及全部用例的后置')class Test03:def test_01(self):print('测试百里守约')def test_02(self, my_fixture):print('测试娜可露露')def test_03(self):print('测试蔡文姬')
生成前后置:

2、autouse自动使用
import pytest@pytest.fixture(scope="function", autouse=True)def my_fixture():print('\n前置方法,可以实现部分及全部用例的前置')yieldprint('\n后置方法,可以实现部分及全部用例的后置')class Test03:def test_01(self):print('测试百里守约')def test_02(self):print('测试娜可露露')def test_03(self):print('测试蔡文姬')
所有方法都加上前后置:

如果改成class
@pytest.fixture(scope="class", autouse=True)
3、params,用例可以使用提前准备的参数
import pytest@pytest.fixture(params=['成龙','甄子丹'] )def my_fixture(request):print('\n前置')yield request.paramprint('\n后置')class Test03:def test_01(self, my_fixture):print('测试百里守约 '+my_fixture)def test_02(self):print('测试娜可露露')def test_03(self):print('测试蔡文姬')

4、name,取个别名
import pytest@pytest.fixture(params=['成龙','甄子丹'], name="wufantest")def my_fixture(request):print('\n前置')yield request.paramprint('\n后置')class Test03:def test_01(self, wufantest):print('测试百里守约 '+wufantest)def test_02(self):print('测试娜可露露')def test_03(self):print('测试蔡文姬')

三、通过conftest.py和@pytest.fixture()结合使用实现全局的前置应用
conftest.py文件是单独存放的一个夹具配置文件,名称不能更改。
用处可以在不同的py文件中使用同一个fixture函数。
原则上conftest.py需要和运行的用例放到统一层。并且不需要做任何的imprt导入的操作。
conftest.py:
import pytestdef read_data():return ['sql111','sql222']@pytest.fixture(params=read_data(), name="wufantest")def execute_sql(request):print('\n执行sql')yield request.paramprint('\n关闭数据库连接')
用例类:
import pytestclass Test04:def test_01(self,wufantest):print('测试百里守约'+wufantest)def test_02(self):print('测试娜可露露')def test_03(self):print('测试蔡文姬')
四、总结
setup/teardown,setup_class/teardown_class 它是作用于所有用例或者所有的类
@pytest.fixtrue() 它的作用是既可以部分也可以全部前后置。
conftest.py和@pytest.fixtrue()结合使用,作用于全局的前后置。
学习资源分享
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….
如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加入下方我的qq群大家一起讨论交流,里面也有各种软件测试资料和技术交流。
边栏推荐
- Nude speech - lying flat - brushing questions - big factory (several tips for Android interviews)
- 【MySQL】使用MySQL Workbench软件新建表
- MVCC详解
- 强化学习_07_DataWhale深度Q网络进阶技巧
- Bigder:42/100 showCase多少bug可以打回去
- pytest之parametrize参数化
- Basic use of Log4j2
- 3.1-3.3 读书笔记
- 各位大佬 oracle cdc 默认配置 偶发会30秒才抓取到数据 这个怎么优化啊
- 强化学习_12_Datawhale深度确定性策略梯度
猜你喜欢

Data types for database learning

High quality WordPress download station 5 play theme template

金融证券 初级 招股书 要求 黑话1刷数 黑话2底稿 黑话3董监高

34. 谈谈为什么要拆分数据库?有哪些方法?
![[网络安全]实操AWVS靶场复现CSRF漏洞](/img/7f/f08e429e3d8ede03a1c1754e256f99.png)
[网络安全]实操AWVS靶场复现CSRF漏洞

Confluence可以连接数据库但是在下一步就报错了

Tencent Cloud Song Xiang: Kubernetes cluster utilization improvement practice

添加spark的相关依赖和打包插件(第六弹)

腾讯云宋翔:Kubernetes集群利用率提升实践

【电商业务】外行为何难区别 商品属性与商品规格
随机推荐
COLMAP+OpenMVS实现物体三维重建mesh模型
动态规划——从0-1背包问题到leetcode正则匹配
Tencent Cloud Song Xiang: Kubernetes cluster utilization improvement practice
Deep understanding of the array
oracle业务表的数据发生增删改,该表的索引会写redo,undo吗?
Big guy, when Oracle single-table incremental synchronization, the source database server takes up nearly 2g of memory. This is not normal, right?
A few lines of code can crash the system;
浅谈C语言整型数据的存储
请问为什么sqlserver cdc,任务启动过了一天,会报这个错误,明明已经开启cdc了。
[Network Security] Practice AWVS Range to reproduce CSRF vulnerability
Qt中输入框在Win10上“Win+/“快捷键的一个Bug
3.1-3.3 读书笔记
Parallax Mapping: More Realistic Texture Detail Representation (Part 1): Why Use Parallax Mapping
结构体初阶
【8月9日活动预告】Prometheus峰会
Qt滚动条(QScrollBar)圆角样式问题跟踪
关于数据库中的中文模糊检索探讨
Qt信号槽与事件循环的关系
WooCommerce 安装和 rest api 使用
数据库学习之数据类型