当前位置:网站首页>Pytest fixture decorator
Pytest fixture decorator
2022-07-26 10:55:00 【Yasso won't blow】
fixture Decorator 5 The meanings of the parameters are explained in detail
fixture Decorator 5 The meanings of the parameters are explained in detail
@pytest.fixture(scope='',autouse='',params="",ids="",name="")
scope :
function Function level Every function or method calls
class Class level Each test class runs only once
module Module level every last .py File call once
session Session level You only need to run once per session , All methods and classes in the session , Modules share this method
autouse :
Automatic execution , Default False, Set to True All use cases included in the later set execution level will execute this method
params:
A parameterized
ids:
When parameterizing, set the variable name for each value
name:
Alias the marked method
scope Parameters use
Code
@pytest.fixture(scope='function')
def my_fixture():
print(" Prepositive method ")
# By default, there is only front , Post setting required yield Division
yield
print(' The rear method ')
class TestFrontBack:
def test_login(self):
print(' Click login ')
print(' Enter account ')
print(' Input password ')
print(' Click login ')
def test_buy(self,my_fixture):
print(' Enter the account and password in the login interface ')
print(' Click login ')
print(' View items and add them to the shopping cart ')
print(' Shopping cart purchase payment goods ')
print(' Check the account balance ')
(function) Method level use

If you want to 2 Both methods execute the pre and post values , You can add (self,my_fixture), But more is troublesome
Use autouse Automatic execution , Before and after values of all method level use cases

(class) Class level uses

(module) Module level ( One py file ) Do not use
The code adds a class for testing
@pytest.fixture(scope='module',autouse=True)
def my_fixture():
print(" Prepositive method ")
# By default, there is only front , Post setting required yield Division
yield
print(' The rear method ')
class TestFixture:
def test_login(self):
print(' Click login ')
print(' Enter account ')
print(' Input password ')
print(' Click login ')
def test_buy(self):
print(' Enter the account and password in the login interface ')
print(' Click login ')
print(' View items and add them to the shopping cart ')
print(' Shopping cart purchase payment goods ')
print(' Check the account balance ')
class TestFixture2:
def test(self):
print(" test module Level execution , That's one py The file is only executed once ")
Execution results 
params A parameterized
Code
@pytest.fixture(scope='function',params=['1234','123477','1234785'])
Fixed writing
def my_fixture(request):
return request.param
class TestFixture:
def test_login(self,my_fixture):
print(' Click login ')
print(' Enter account ')
print(' Input password '+str(my_fixture))
print(' Click login ')
Execution results

Parameterization is used with pre and post
Use yield Instead of return Use the return parameter 
ids Parameters : Give the parameter a variable name

name Parameters to fixture Alias the marked method
Be careful : After taking the alias , Can't find with your real name fixture Method of marking 
边栏推荐
- Sql Server之查询总结
- Sql Server 之SQL语句对基本表及其中的数据的创建和修改
- 2021-08-13 learn C language with pengge - array
- list升序和降序
- ThreadPoolExecutor是怎样执行任务的
- Disable usbjatg in Altium Designer
- How to assemble a registry?
- 35. Search the insertion position
- 0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突
- 回到顶部的几种方案(js)
猜你喜欢

pytest 前后置方法

nmap弱点扫描结果可视化转换

The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly

SparkSQL的UDF及分析案例,220725,

RT thread learning notes (I) -- configure RT thread development environment

菜鸟看源码之HashTable

WIRESHARK基础教程以太帧的分析。

344. Reverse string

很多人都不清楚自己找的是Kanban软件还是看板软件

为什么需要自动化测试?软件测试师带你测评不同软件测试工具
随机推荐
35. Search the insertion position
Sql Server 数据库之数据类型
Sword finger offer (8): jump the steps
微信公众号消息通知 “errcode“:40164,“errmsg“:“invalid ip
Sword finger offer (53): a string representing a numeric value
按二进制数中1的个数分类
logging基本使用
Bash shell learning notes (4)
shell 脚本 失败自动重复执行
二叉树的遍历 递归+迭代
C语言命名空间的定义与使用
Fragment 懒加载
Bash shell学习笔记(三)
104.二叉树的最大深度
Minesweeping Pro version 2021-08-19
@NotBlank、@NotNull 、@NotEmpty 区别和使用
MFC图片控件
postman 导出导入
Bigdecimal的加减乘除、比较大小、向上向下取整 和 Bigdecimal的集合累加、判断BigDecimal是否有小数
list升序和降序