当前位置:网站首页>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 :
 Insert picture description here

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 :
 Insert picture description here

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 :
 Insert picture description here
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 ')
		...............
		...............

 Insert picture description here
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 :
 Insert picture description here

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 :
 Insert picture description here
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 :
 Insert picture description here

原网站

版权声明
本文为[Expert of explosive liver fist]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211318464965.html