当前位置:网站首页>自动化测试框架Pytest(二)——前后置处理
自动化测试框架Pytest(二)——前后置处理
2022-08-10 06:49:00 【测试小娜】
有一些初始化配置和测试之后的收尾,只需要处理一次,这个时候我们就要用到夹具。
一、pytest的前后置(固件、夹具)处理
1、setup/teardown,setup_class/teardown_class
setup/teardown:在每个用例的前后都会执行
setup_class/teardown_class:在每个类的前后都会执行
import pytest
class 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、package
params:参数化
autouse:True自动执行,默认是False
ids:当使用params参数化时,给每一个值设置一个变量名,意义不大
name:给标记的方法取一个别名
1、scope,默认是function
import pytest
@pytest.fixture(scope="function")
def my_fixture():
print('\n前置方法,可以实现部分及全部用例的前置')
yield
print('\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前置方法,可以实现部分及全部用例的前置')
yield
print('\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.param
print('\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.param
print('\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 pytest
def read_data():
return ['sql111','sql222']
@pytest.fixture(params=read_data(), name="wufantest")
def execute_sql(request):
print('\n执行sql')
yield request.param
print('\n关闭数据库连接')
用例类:
import pytest
class 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群大家一起讨论交流,里面也有各种软件测试资料和技术交流。
边栏推荐
- C language file operation
- Chapter 12 Other Database Tuning Strategies [2. Index and Tuning] [MySQL Advanced]
- Ladies and gentlemen, oracle11g, cdc2.2, flink1.13.6, single-table incremental synchronization.Without adding data
- 【Day10】进程管理命令
- 【8月9日活动预告】Prometheus峰会
- 杭州公积金修改手机号信息
- 2022 Henan Mengxin League No. 5: University of Information Engineering J-AC Automata
- 语法基础(判断语句)
- Fiddler(八) - 抓取手机APP的流量-插件Fiddler Orchestra Beta安装&配置
- The constraints of the database learning table
猜你喜欢
第12章 数据库其它调优策略【2.索引及调优篇】【MySQL高级】
Grammar Basics (Judgment Statements)
About MongoDb query Decimal128 to BigDecimal problem
Tencent Cloud Song Xiang: Kubernetes cluster utilization improvement practice
Chapter 11 Database Design Specifications [2. Index and Tuning] [MySQL Advanced]
Text-to-Image最新论文、代码汇总
【MySQL】SQL语句
MySQL's InnoDB engine (6)
腾讯云宋翔:Kubernetes集群利用率提升实践
Elementary Structure
随机推荐
2022 Henan Mengxin League (fifth) game: University of Information Engineering H - Xiao Ming drinking milk tea
杭州公积金修改手机号信息
Log4j2基本使用
3. Transactions [mysql advanced]
【Day10】进程管理命令
数据库学习之数据类型
强化学习_07_DataWhale深度Q网络进阶技巧
个人实现的可任意折叠QToolBox——AdvancedToolBox
高级测试:如何使用Flink对Strom任务的逻辑功能进行复现测试?
【电商业务】外行为何难区别 商品属性与商品规格
阿里巴巴(中国)网络技术有限公司、测试开发笔试二面试题(附答案)
力扣(LeetCode)221. 最大正方形(2022.08.09)
如何治理资源浪费?百度云原生成本优化最佳实践
【强化学习】《Easy RL》- Q-learning - CliffWalking(悬崖行走)代码解读
Nude speech - lying flat - brushing questions - big factory (several tips for Android interviews)
Chapter 12 Other Database Tuning Strategies [2. Index and Tuning] [MySQL Advanced]
QScroller的QScrollerProperties参数研究
761. Special Binary Sequences
各位大佬,oracle11g,cdc2.2,flink1.13.6,单表增量同步。在没新增数据的情
May I ask why sqlserver cdc will report this error one day after the task is started, obviously cdc has been opened.