当前位置:网站首页>pytest fixture装饰器
pytest fixture装饰器
2022-07-26 10:50:00 【亚索不会吹风】
fixture装饰器5个参数含义详解
fixture装饰器5个参数含义详解
@pytest.fixture(scope='',autouse='',params="",ids="",name="")
scope :
function 函数级 每一个函数或方法都会调用
class 类级别 每个测试类只运行一次
module 模块级 每一个.py文件调用一次
session 会话级 每次会话只需要运行一次,会话内所有方法及类,模块都共享这个方法
autouse :
自动执行,默认False,设置为True后再设置的执行等级里面包含的所有用例都会执行这个方法
params:
参数化
ids:
参数化时给每一个值设置变量名
name:
给被标记的方法取别名
scope 参数使用
代码
@pytest.fixture(scope='function')
def my_fixture():
print("前置方法")
#默认只有前置,需要后置设置yield分割
yield
print('后置方法')
class TestFrontBack:
def test_login(self):
print('点击登录')
print('输入账号')
print('输入密码')
print('点击登录')
def test_buy(self,my_fixture):
print('登录界面输入账号密码')
print('点击登录')
print('查看商品加入购物车')
print('购物车购买支付商品')
print('查看账户余额')
(function)方法级别使用

如果想2个方法都执行这个前后值,可以在每个方法里面加上(self,my_fixture),但是多了就很麻烦
使用autouse自动执行,所有方法级的用例的前后值

(class)类级别使用

(module)模块级(一个py文件)别使用
代码加了一个类用于测试
@pytest.fixture(scope='module',autouse=True)
def my_fixture():
print("前置方法")
#默认只有前置,需要后置设置yield分割
yield
print('后置方法')
class TestFixture:
def test_login(self):
print('点击登录')
print('输入账号')
print('输入密码')
print('点击登录')
def test_buy(self):
print('登录界面输入账号密码')
print('点击登录')
print('查看商品加入购物车')
print('购物车购买支付商品')
print('查看账户余额')
class TestFixture2:
def test(self):
print("测试module级别执行,也就是一个py文件里面只执行一次")
执行结果
params参数化
代码
@pytest.fixture(scope='function',params=['1234','123477','1234785'])
固定写法
def my_fixture(request):
return request.param
class TestFixture:
def test_login(self,my_fixture):
print('点击登录')
print('输入账号')
print('输入密码'+str(my_fixture))
print('点击登录')
执行结果

参数化和前后置一起使用
使用yield代替return使用返回参数
ids参数:给参数取变量名

name参数 给fixture标记的方法取别名
注意:取了别名后,无法用本名找到fixture标记的方法
边栏推荐
- 智能合约dapp系统开发流程技术
- Sword finger offer (53): a string representing a numeric value
- RT thread learning notes (V) -- edit, download and debug programs
- What are the biz layer and manager layer in the company project
- Wechat official account message notice "errCode": 40164, "errmsg": "invalid IP
- 使用Selenium抓取zabbix性能监控图
- 349. 两个数组的交集
- px2rem-loader将px转化为rem,适配移动端vant-UI等框架
- Bash shell学习笔记(六)
- 104.二叉树的最大深度
猜你喜欢

如何组装一个注册中心?

Drbl diskless startup + Clonezilla network backup and restore system

RT thread learning notes (V) -- edit, download and debug programs

Traversal recursion + iteration of binary tree

Why do I need automated testing? Software testers take you to evaluate different software testing tools

RT thread learning notes (III) -- building a compilation environment with scons

0x00007ffd977c04a8 (qt5sqld.dll) (in a.exe): 0xc0000005: an access violation occurred when reading position 0x0000000000000010

SCADA和三大工业控制系统PLC、DCS、FCS

菜鸟看源码之HashTable

Kali view IP address
随机推荐
Bash shell学习笔记(二)
Pengge C language 20210811 program structure operation
Tutorial of putty
Linkedblockingqueue of novice source code
Pengge C language - minesweeping 2021-08-16
MFC picture control
Newbie sees the source code arraydeque
菜鸟看源码之HashTable
242. Effective letter heteronyms
【小程序】onReachBottom 事件为什么不能触发 ?(一秒搞定)
display-inline+calc实现左中右布局,中间自适应
二叉树的遍历 递归+迭代
解决org.apache.commons.codec.binary.Base64爆红问题
Fragment lazy load
使用flex实现左中右布局,中间自适应
RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)
Constructors, method overloads, object arrays, and static
c结构体中定义的成员指针赋值与结构体指针作为成员函数参数的使用
菜鸟看源码之LinkedBlockingQueue
按二进制数中1的个数分类