python-pytest测试框架中的setup和teardown

一、写在前面

同unittest 测试框架一样 ,pytest 也提供了 类似 setup 和 teardown 的初始化方法和结束后的方法,且pytest 提供了更为细致的 方法

  1. setup_module / teardown_module  模块级别 
  2. setup_function / teardown_function 函数级别  (包含测试类中的静态方法)
  3. setup_class / teardown_class     类级别的
  4. setup_method / teardown_method  类方法级别的  (执行优先级高于 setup / teardown,两种方法一般取其中一种即可)
  5. setup / teardown  类方法级别的 

 

二、代码

1、setup&teardown

方法级别的初始化方法,----》定义在类中
setup 和 setup_method 两个差不多 大部分情况下使用其中一个即可 ,但优先级
setup_method/teardown_method 执行的优先级 高于 setup/teardown
会在模块中的每个测试类中的每个测试方法(包含静态方法)
执行前执行该初始化和结束方法
执行优先级:
在静态方法中    setup<setup_method<setup_function
在类方法中     setup<setup_method

import pytest

def setup_function():
    print('setup function-----------')

def teardown_function():
    print('teardown function----------')class Test_Module1:

    def setup_method(self):
        print('setup_method-------')

    def teardown_method(self):
        print('teardonw_method-------')

    def setup(self):
        print('setup-----------------')

    def teardown(self):
        print('teardonw--------------')

    def test_1(self):
        print('第一个类的测试方法1')

    def test_2(self):
        print('第一个类的测试方法2')

    # 类中的静态方法也是函数,
    @staticmethod
    def test_static():
        print('这个是一个类的静态方法-------')
    # 类方法不会作为测试方法执行
    @classmethod
    def test_cls(cls):
        print('这个是一个类方法-----')

def test_function():
    print('这个是函数1')


def test_function2():
    print('这个是函数2')

if __name__ == '__main__':
    pytest.main(['-vs','./setup_demo.py'])

 

2、setup_method&teardown_method

方法级别的初始化方法,----》定义在类中
setup_method & teardown_method会在模块中的每个测试类中的每个测试方法(包含静态方法)
执行前执行该初始化和结束方法
执行优先级:
在静态方法中    setup<setup_method<setup_function
在类方法中     setup<setup_method

import pytest,time

def setup_function():
    print('setup function-----------')

def teardown_function():
    print('teardown function----------')

def setup_method():
    print('outside setup_method-------')

def teardown_method():
    print('outside teardown_method-------')

class Test_Module1:

    def setup_method(self):
        print('inside setup_method-------')

    def teardown_method(self):
        print('inside teardonw_method-------')


    def test_1(self):
        print('第一个类的测试方法1')

    def test_2(self):
        print('第一个类的测试方法2')

    # 类中的静态方法 也会被作为测试方法执行
    # 可以被setup_method  setup_function 执行,
    # setup_function 执行优先级高于  setup_method
    @staticmethod
    def test_static():
        print('这个是一个静态方法')
    # 类方法不会作为测试方法
    @classmethod
    def test_cls(cls):
        print('这是一个类方法')


def test_function():
    print('这个是外部函数1')


def test_function2():
    print('这个是外部函数2')


if __name__ == '__main__':
    pytest.main(['-v','./setup_method.py'])

 

3、setup_function&teardown_function

函数级别的初始化方法,定义在类的外面
会在模块中的每个函数(只会执行类外面的函数),执行前执行该初始化和结束方法
特别注意 类中方法 执行前不会执行 函数级别的初始化方法
执行优先级:
在静态方法中 setup<setup_method<setup_function

import pytest

def setup_function():
    print('整个函数级别的初始化方法'+'+'*10)

def teardown_function():
    print('整个函数级别的结束方法'+'-'*10)


class Test_Module1:
def test_1(self): print('第一个类的测试方法1') def test_2(self): print('第一个类的测试方法2') class Test_Module2: def test_11(self): print('第二个类的测试方法1') def test_22(self): print('第二个类的测试方法1') # 类中的静态方法也是 函数, @staticmethod def test_static(): print('这个是第二个类中的静态方法') @classmethod def test_cls(cls): print('这个是第二个类中的类方法22222222222222') def test_function(): print('这个是函数1') def test_function2(): print('这个是函数2') if __name__ == '__main__': pytest.main(['-v','./setup_module.py'])

 

4、setup_class&teardown_class

类级别的初始化方法,----》定义在类中
会在模块中的每个测试类执行前执行该初始化和结束方法

import pytest

def setup_function():
    print('setup_function--------')

def teardown_function():
    print('teardown_function--------')

class Test_Module1:

    def setup_class(self):
        print('setup_class-----------')

    def teardown_class(self):
        print('teardown_class----------')

    def test_1(self):
        print('这是类的测试方法1')

    def test_2(self):
        print('这是类的测试方法2')
# 静态方法,也会被作为测试方法 @staticmethod def test_static(): print('这是一个静态方法-------')
# 类方法不会被作为测试方法 @classmethod def test_cls(cls): print('这是一个类方法--------') def test_function(): print('这个是函数1') def test_function2(): print('这个是函数2') if __name__ == '__main__': pytest.main(['-vs','./setup_class.py'])

 

5、setup_module&teardown_module

模块级别的初始化方法,定义在类的外面
整个测试模块执行前后会执行初始化和结束方法

(或者说当前py文件执行前后会执行一次)

import pytest

def setup_module():
    print('整个模块级别的初始化方法')

def teardown_module():
    print('整个模块级别的结束方法')

class Test_Module:

    def test_1(self):
        assert 1==2

    def test_2(self):
        assert 2==2

class Test_Module1:
def test_11(self): assert 1 == 2 def test_22(self): assert 2 == 2 def test_function(): assert 'k' in 'fuck' if __name__ == '__main__': pytest.main(['-v','./setup_module.py'])

 

posted @ 2021-12-04 21:08  昆虫白  阅读(929)  评论(0)    收藏  举报