当前位置:网站首页>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 .
边栏推荐
- Gorilla/mux framework (RK boot): add swagger UI
- Optimization method of live weak network
- How does the easyplayer streaming video player set up tiling?
- How audio and video technology provides a completely true and stable new experience for the meta universe
- A bit about the state machine (FSM SMR DFSM)
- February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
- New uniapp+uniui background management uniuadmin
- Add other view components to the audio and video components of the applet
- How to customize a finished label template
- Cve-2021-4034 reappearance
猜你喜欢

5. concept of ruler method

Vulnhub DC-5

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

6. template for integer and real number dichotomy

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030
What is sitelock? What is the function?

8. greed

How to store, manage and view family photos in an orderly manner?

C language series - Section 4 - arrays
随机推荐
RI Geng series: potential memory leak caused by abuse of mutable in Google Pb structure
How to gracefully solve the problem of platform font adaptation
Precision loss problem
Summary of easy-to-use MySQL interview questions (Part 1)
Implementation idea and solution of calling global monitoring for applet
Win11 client remote ECS black screen
An article shows you the difference between high fidelity and low fidelity prototypes
DNS Service Setup
Canvas draw the clock
Exploration on the framework of stream batch integration technology and its practice in kangaroo cloud number stack
PNAs: power spectrum shows obvious bold resting state time process in white matter
How to batch print serial and repeated barcode data
Xiamen's hidden gaopuge smart park has finally been uncovered
method
The commercial s2b2b e-commerce platform of aquatic industry improves the competitiveness of enterprises and creates a strong engine for industrial development
How to batch generate matrix 25 codes
Detailed explanation of online reputation management
Delta oscillation in EEG
Blue screen and abnormal downtime DMP file and system log collection
Web components series (I) - Overview