当前位置:网站首页>Pytest framework implements pre post processing
Pytest framework implements pre post processing
2022-06-21 17:02:00 【Expert of explosive liver fist】
One 、setup/teardown
By adding setup And teardown Method , Make the Each use case Both before and after .
Case code :
class TestAA:
def setup(self):
print(' Start --------')
@pytest.mark.smoke
def testaa(self):
time_loc=time.strftime('%H-%M-%S',time.localtime())
time.sleep(2)
print(time_loc+' Hello! bb ah ')
@pytest.mark.run(order=1) # Indicates in the execution order , The first 1 To execute
def testbb(self):
time_loc=time.strftime('%H-%M-%S',time.localtime())
print(time_loc+' Hello! aa ah ')
def teardown(self):
print(' end -----------------\n')
Running results :
Two 、setup_class/teardown_class
stay Before all use cases Only once
Use scenarios :
Initialization before and after each class execution , Such as : establish / Destroy log object , establish / Destroy connection to database , establish / Destroy the request object of the interface .
Case code :
class TestAA:
def setup_class(self):
print(' Before class execution ---')
....................
....................
def teardown_class(self):
print(' After class execution ------')
Running results :
3、 ... and 、 Use fixture Decorator to achieve part Pre post of use case
Format :@pytest.fixture(scope=' ',params='',autouse='',ids='',name='')
(1)scope: It means being @pytest.fixture Method of marking (function, Default this value ), class (class), modular (module) Or bag (package/session)
(2)params: A parameterized ( Support List [ ], Tuples (), Dictionary list [{},{}], Dictionary tuples ({},{},{}))
(3)autouse=True: Automatic use , The default value is False
(4)ids: When using params When parameterizing , Set a variable name for each value
(5)name: Show to be @pytest.fixture Take an alias for the marked function name
1. Pre post for each method
Code :
@pytest.fixture(scope='function',autouse='true')
def my_fixture():
print('fixture The pre - ')
yield # Indicates post
print('fixture The back of ')
class TestAA:
@pytest.mark.smoke
def testaa(self):
time_loc=time.strftime('%H-%M-%S',time.localtime())
time.sleep(2)
print(time_loc+' Hello! bb ah ')
...............
...............
Running results :
2. Pre and post a specified method
Code :
@pytest.fixture(scope='function')
def my_fixture():
print('fixture The pre - ')
yield # Indicates post
print('fixture The back of ')
class TestAA:
@pytest.mark.smoke
# This method is specified before and after
def testaa(self,my_fixture):
time_loc=time.strftime('%H-%M-%S',time.localtime())
time.sleep(2)
print(time_loc+' Hello! bb ah ')
...............
...............

3. Realize parameterization
@pytest.fixture(scope='function',params=[' Liuganming ',' Oho ',' Donnie Yen '])
def my_fixture(request):
return request.param #(!! Pay attention to this param No, s)
#(!!!return and yield They all mean to return , however return There must be no code behind the ,yield Code can be followed .)
class TestAA:
@pytest.mark.smoke
def testaa(self,my_fixture):
time_loc=time.strftime('%H-%M-%S',time.localtime())
time.sleep(2)
print(time_loc+' Hello! bb ah ')
print('.................'+str(my_fixture))
Running results :
Four 、 adopt conftest.py Module and @pytest.fixture() Combined use to achieve overall situation Pre application of
1.conftest.py The file is a fixture configuration file stored separately , The name cannot be changed .
2. use : Can be in different py Use the same in the file fixture function .
3. One can be placed under different modules conftest.py The file implements the corresponding module pre post
5、 ... and 、parametrize The basic usage of decorators
@pytest.mark.parametrize(args_name,args_value)
args_name: Parameter name
args_value: Parameter values ( list , Tuples , Dictionary list , Dictionary tuples ), How many use cases will be executed as many times
Example 1( Single data ):
class TestAA:
@pytest.mark.parametrize('name',[' Xiao Ming ',' petty thief ',' Xiao Xiao '])
def testdd(self,name):
print(name)
result :
Example 2( More data ):
class TestAA:
@pytest.mark.parametrize('name,sex',[ [' Xiao Ming ',' male '],[' petty thief ',' Woman '],[' Xiao Xiao ',' manly woman '] ])
def testdd(self,name,sex):
print(name,sex)
result :
边栏推荐
- [live broadcast preview] at 19:00 on June 24, hcsd live broadcast -- employment guide, which will take you through the interview points for the upcoming autumn recruitment and summer internship~~
- Unittest框架的测试日志
- 重磅丨国内首份呈现数据库发展历程的图鉴正式发布!
- The Google play academy team PK competition officially begins!
- Show you how to distinguish several kinds of parallelism
- About SQL: does anyone in SQL know how to answer these questions?
- Simulation Implementation of string class
- Kindeditor uploading pictures and using
- 聚焦工业智能化场景,华为云招募合作伙伴,帮助解决转型难题
- 带你区分几种并行
猜你喜欢

7 tips for writing effective help documents

The "learning link" database of the learning software is suspected to have leaked information, revealing more than 100million pieces of student information

数据分析必备:6大步骤+5大类型+2大分析方法

I do 3D restoration for the aircraft carrier: these three details are shocking

Google Play Academy 组队 PK 赛,正式开赛!

在线文本列表批量添加行号工具

Huawei cloud releases desktop ide codearts

学习软件“学习通”数据库疑似发生信息泄露,泄露学生信息达1亿多条

In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened

HUAWEI(13)——路由引入
随机推荐
Wechat applet development tutorial - Introduction to text components
Huawei cloud releases desktop ide codearts
站在数字化风口,工装企业如何“飞起来”
Online JSON to yaml tool
[理论]--接口测试
qtcreator报错解决
Any to Any 实时变声的实现与落地丨RTC Dev Meetup
如何编写测试用例
2021数据库市场,Aerospike与顶级厂商争锋
【1108. IP 地址無效化】
使用 Guzzle 中间件进行优雅的请求重试
About SQL: does anyone in SQL know how to answer these questions?
Reinforcement learning introductory project spinning up (1) installation
Why do you want to develop tea mall applet app?
撰写有效帮助文档的7大秘诀
Notice on Revising the guidelines for the planning, design and livable construction of housing with common property rights in Beijing (for Trial Implementation)
KindEditor上传图片及使用
海瑞科技完成数千万元Pre-A轮融资,打造国内首个人工智能配电物联网
PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
[live broadcast preview] at 19:00 on June 24, hcsd live broadcast -- employment guide, which will take you through the interview points for the upcoming autumn recruitment and summer internship~~