当前位置:网站首页>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 .

原网站

版权声明
本文为[Meccer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201251808181148.html