当前位置:网站首页>自动化测试框架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群大家一起讨论交流,里面也有各种软件测试资料和技术交流。
边栏推荐
猜你喜欢
结构体初阶
ES13 - ES2022 - The 123rd ECMA Congress approves the ECMAScript 2022 language specification
CuteOneP is a PHP-based OneDrive multi-network disk mount program with member synchronization and other functions
【Day10】进程管理命令
如何正确理解线程机制中常见的I/O模型,各自主要用来解决什么问题?
【强化学习】《Easy RL》- Q-learning - CliffWalking(悬崖行走)代码解读
[Reinforcement Learning] "Easy RL" - Q-learning - CliffWalking (cliff walking) code interpretation
ATH10传感器读取温湿度
【Event Preview on August 9】Prometheus Summit
Qt借助隐藏控件和QSS绘制重复元素
随机推荐
关于MongoDb查询Decimal128转BigDecimal问题
MVCC详解
浅谈C语言实现冒泡排序
各位大佬,oracle11g,cdc2.2,flink1.13.6,单表增量同步。在没新增数据的情
Quickly grasp game resources in one hour and remote hot update
基于STC8G2K64S4单片机通过OLED屏幕显示模拟量光敏模拟值
mysql之两阶段提交
【无标题】
数据库学习之表的约束
I would like to ask you guys, when FLink SQL reads the source, specify the time field of the watermark. If the specified field is in the grid
3.1-3.3 读书笔记
Text-to-Image最新论文、代码汇总
2022 Henan Mengxin League (fifth) game: University of Information Engineering H - Xiao Ming drinking milk tea
Reproduce dns out-band data combined with sqlmap
BUUCTF Notes (web)
【MySQL】SQL语句
[Network Security] Practice AWVS Range to reproduce CSRF vulnerability
JS中初始化对象为null和空对象的区别
Nude speech - lying flat - brushing questions - big factory (several tips for Android interviews)
英国国家卫生服务遭受攻击,系统出现大面积故障