Pre and post processing of pytest

Everyone who has used unittest knows that setup and teardown are used to handle the work before and after the start of use cases. Among them, setupclass and teardown class are used to ensure the execution. Therefore, the use cases are only executed once before and after, which is very convenient to use. Therefore, learning the powerful test frame of pytest must also have this function, And it's much simpler than unittest.

Prepositions in pytest

pytest is relatively powerful and provides more than one pre and post method:

  • setup_module,teardown_module 
  • setup_function,teardown_function 
  • setup_class,teardown_class 
  • setup_method,teardown_method
  • setup,teardown 

Just looking at the content above, I'm sure I'm confused. I don't know when to use it. I'll introduce it one by one through examples

setup,teardown

First, let's introduce the first one we are familiar with and the writing in unittest. This one can be used in or out of class.

This method is executed for each use case

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('Case 01')

def test02():
    print('Case 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. In the process, it is only executed once. It is equivalent to the setupclass and teardownclass methods in unittest

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('Case 01')

def test02():
    print('Case 02')

if __name__ == '__main__':
    pytest.main(['-s','test_02.py'])

setup_function,teardown_function

This method means that in the process of executing the use case outside the class, the pre and post will be executed every 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('Case 01')

def test02():
    print('Case 02')

if __name__ == '__main__':
    pytest.main(['-s','test_02.py'])

setup_method,teardown_method

This method means that before each test case is executed in the class, the pre test and post test will be executed once

# coding:utf-8
import pytest

class Test():

    def setup_method(self):
        print('This is setup Function pre content')

    def teardown_method(self):
        print('This is teardown Function post content')

    def test01(self):
        print('This is test case 1')

    def test02(self):
        print('This is test case 2')


if __name__ == '__main__':
    pytest.main(['-s','test_01.py'])

setup_class,teardown_class

This method means that the pre test and post test are only executed once before the test case is executed in the class

# coding:utf-8
import pytest

class Test():

    def setup_class(self):
        print('This is setup Function pre content')

    def teardown_class(self):
        print('This is teardown Function post content')

    def test01(self):
        print('This is test case 1')

    def test02(self):
        print('This is test case 2')


if __name__ == '__main__':
    pytest.main(['-s','test_01.py'])

setup_class and setup_ Mixed method and setup

import pytest

class Test():
    def setup_method(self):
        print('This is setup_method Pre use case content')

    def setup_class(self):
        print('This is setup_class Pre use case content')

    def setup(self):
        print('This is setup Pre use case content')

    def teardown_class(self):
        print('This is teardown_class Post use case content')

    def teardown_method(self):
        print('This is teardown_method Post use case content')

    def teardown(self):
        print('This is teardown Post use case content')

    def test01(self):
        print('This is test case 1')

    def test02(self):
        print('This is test case 2')


if __name__ == '__main__':
    pytest.main(['-s','test_01.py'])

During ui automation, we often take starting the browser as the front, so that the browser can be started only once when the use case is executed, so we need to process each class only once. The following is the actual combat 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_ When the module executes the use case, it only executes the pre and post functions once

2,setup_class,setup_method and setup are executed in the class

3,setup_module,setup_function and setup are executed outside the class

4. In the setup class, it can be executed outside the class.

Added by keigowei on Thu, 27 Jan 2022 11:41:55 +0200