当前位置:网站首页>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 
边栏推荐
- MFC picture control
- postman 导出导入
- 企鹅龙(DRBL)无盘启动+再生龙(clonezilla)网络备份与还原系统
- Traversal recursion + iteration of binary tree
- Bash shell学习笔记(二)
- RT thread learning notes (V) -- edit, download and debug programs
- MFC多线程的简单使用
- Implementing queues with two stacks
- list升序和降序
- Pengge C language 20210811 program structure operation
猜你喜欢

Bash shell学习笔记(六)

ESXi6.5补丁更新

Classified by the number of 1 in binary number

Wechat official account message notice "errCode": 40164, "errmsg": "invalid IP

20210807 1 c language program structure

SCADA and three industrial control systems PLC, DCS and FCS

Sql Server 数据库之数据类型

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

Add touch screen driver for stemwin 5.22 on Shenzhou IV development board

Bash shell学习笔记(四)
随机推荐
Sql Server之查询总结
菜鸟看源码之SparseArray
PLC与伺服电机连接
232. Implement queue with stack
Fragment lazy load
349. Intersection of two arrays
@The difference and use of jsonformat and @datetimeformat
Bash shell learning notes (II)
Happens-Before原则深入解读
WIRESHARK基础教程以太帧的分析。
35. 搜索插入位置
SCADA和三大工业控制系统PLC、DCS、FCS
-bash: ./build.sh: /bin/bash^M: 坏的解释器: 没有那个文件或目录
Sql Server 数据库之数据类型
RT thread learning notes (I) -- configure RT thread development environment
BLE之ATT请求
Pengge C language - minesweeping 2021-08-16
2021-08-12 function recursion_ Learn C language with brother Peng
@NotBlank、@NotNull 、@NotEmpty 区别和使用
Sword finger offer (twenty): stack containing min function