当前位置:网站首页>Pre and post processing of pytest
Pre and post processing of pytest
2022-06-23 03:02:00 【Meccer】
Have used unittest We all know that ,setup and teardown It is used to deal with the work before and after the use case , Among them are setupclass and teardownclass Is to ensure the execution, so all use cases only execute 1 Secondary pre and post , It is very convenient to use , So learning pytest Powerful test frame , It must also have this function , And it's better than unittest It's a lot simpler .
pytest The front in
pytest More powerful , Provides more than one method of pre and post :
- setup_module、teardown_module
- setup_function、teardown_function
- setup_class、teardown_class
- setup_method、teardown_method
- setup、teardown
Just look at the content above , Must be confused , I don't know when to use , Quiet to introduce one by one through examples
setup、teardown
Let's introduce the first one we are familiar with unittest Writing in has been , This can be used in classes , It can also be used outside the class .
Each use case of this method will execute
import pytest
def setup():
print(' This is the front of the test case ')
def teardown():
print(' This is the post test case ')
def test01():
print(' Use cases 01')
def test02():
print(' Use cases 02')
if __name__ == '__main__':
pytest.main(['-s'])setup_module、teardown_module
This method means that the use case can only be executed outside the class , Only execute 1 Time . amount to unittest Medium setupclass and teardownclass Method
import pytest
def setup_module():
print(' This is the front of the test case ')
def teardown_module():
print(' This is the post test case ')
def test01():
print(' Use cases 01')
def test02():
print(' Use cases 02')
if __name__ == '__main__':
pytest.main(['-s','test_02.py'])setup_function、teardown_function
This method represents the process of executing the use case outside the class , Pre and post... Are performed each time .
import pytest
def setup_function():
print(' This is the front of the test case ')
def teardown_function():
print(' This is the post test case ')
def test01():
print(' Use cases 01')
def test02():
print(' Use cases 02')
if __name__ == '__main__':
pytest.main(['-s','test_02.py'])setup_method、teardown_method
This method represents before each execution of the test case in the class , Both pre test and post test will be executed once
# coding:utf-8
import pytest
class Test():
def setup_method(self):
print(' This is a setup Function front content ')
def teardown_method(self):
print(' This is a teardown Function post content ')
def test01(self):
print(' This is the test case 1')
def test02(self):
print(' This is the test case 2')
if __name__ == '__main__':
pytest.main(['-s','test_01.py'])setup_class、teardown_class
This method represents before executing the test case in the class , Only execute 1 Pre test and post test
# coding:utf-8
import pytest
class Test():
def setup_class(self):
print(' This is a setup Function front content ')
def teardown_class(self):
print(' This is a teardown Function post content ')
def test01(self):
print(' This is the test case 1')
def test02(self):
print(' This is the test case 2')
if __name__ == '__main__':
pytest.main(['-s','test_01.py'])setup_class and setup_method、setup blend
import pytest
class Test():
def setup_method(self):
print(' This is a setup_method The front content of the use case ')
def setup_class(self):
print(' This is a setup_class The front content of the use case ')
def setup(self):
print(' This is a setup The front content of the use case ')
def teardown_class(self):
print(' This is a teardown_class Post use case content ')
def teardown_method(self):
print(' This is a teardown_method Post use case content ')
def teardown(self):
print(' This is a teardown Post use case content ')
def test01(self):
print(' This is the test case 1')
def test02(self):
print(' This is the test case 2')
if __name__ == '__main__':
pytest.main(['-s','test_01.py'])ui When automating , We often start the browser as the front , Let the browser start only once when the use case is executed , So we need every class Handle only once , Here is the actual code
class test(BaseCase):
def setup_class (self):
bc=BaseCase()
self.driver =bc.GetDriver("Chrome")
self.driver.get ("https://account.369zhan.com/auth/loginPage")
self.l = LoginPage (self.driver)
def teardown_class (self):
self.driver.quit ()
@pytest.mark.parametrize ('username, password',[ ('13129562261', 'czh123')])
def test_login(self, username, password):
self.l.login(username,password)
time.sleep(2)
@pytest.mark.parametrize ('username, password',[('test1', '123') ])
def test_loginz(self, username, password):
time.sleep (2)
self.l.login(username,password)
summary :
1、setup_class and setup_module When executing use cases , Perform pre and post... Only once
2、setup_class,setup_method,setup Is executed in a class
3、setup_module,setup_function,setup Is executed outside the class
4、 among setup Class , Can be executed outside the class .
边栏推荐
- QUIC or TCP
- Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
- [SaaS examination certification] apaas_ Tencent Qianfan magic pen
- Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030
- Use Sakura FRP intranet penetration service to build your own website / game server
- The metauniverse is just a cloak for future technological evolution
- How to batch make decreasing serial number barcode
- Markdown - mark above / below symbol (typora, latex)
- What is sitelock? What is the function?
- Why can only a small number of condition type prices be maintained in me12 of SAP mm?
猜你喜欢
What is sitelock? What is the function?

5. concept of ruler method

Vulnhub DC-5

C language series - Section 4 - arrays

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

8. greed

6. template for integer and real number dichotomy

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

How to store, manage and view family photos in an orderly manner?
随机推荐
How to make traditional Chinese medicine labels with pictures
Optimization method of live weak network
Build a weather forecast applet using a widget
Reading redis source code (IV) command request processing flow
Summary of website navigation design and website link optimization
Canvas draw the clock
How does the easyplayer streaming video player set up tiling?
Deep analysis of time complexity
Pond sampling
CFs of cifs/smb protocol is mounted on win10/2019. Error 1272 is reported. The security policy prevents unauthenticated guest access
CVE-2021-21973 Vmware Vcenter SSRF POC
January 31, 2022: Maze III. There is a ball in the maze of open spaces and walls. ball
Zoom/skype/ nailing / live broadcast / conference / online video real-time subtitle generation and translation, simultaneous interpretation
February 2, 2022: the closest binary search tree value II. Given a non empty two
3. install and deploy Mgr cluster | explain Mgr in simple terms
A penetration of an internal self built shooting range
How to generate IATA barcode in batch through TXT file
Why do I use index, query or slow?
Quic implementation in rust --- Quinn
Summary of easy-to-use MySQL interview questions (Part 1)