Pytest 测试框架结构L1(setup/teardown)

Pytest 测试框架结构(setup/teardown)

测试装置介绍

类型 规则
setup_module/teardown_module 全局模块级
setup_class/teardown_class 类级,只在类中前后运行一次
setup_function/teardown_function 函数级,在类外
setup_method/teardown_method 方法级,类中的每个方法执行前后
setup/teardown 在类中,运行在调用方法的前后(重点)
'''
模块级(setup_module/teardown_module)模块始末,全局的(优先最高)
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_methond)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后
'''

# 模块级别,只被调用一次
def setup_module():
    print("资源准备:setup module")


def teardown_module():
    print("资源准备:teardown module")


def test_case1():
    print("case1")

def test_case2():
    print("case2")


def setup_function():
    print("资源准备:setup function")


def teardown_function():
    print("资源消毁:teardown function")


class TestDemo:

    #  执行类 前后分别执行setup_class  teardown_class
    def setup_class(self):
        print("TestDemo setup_class")

    def teardown_class(self):
        print("TestDemo teardown_class")

    # 每个类里面的方法前后分别执行 setup, teardown
    def setup(self):
        print("TestDemo setup")

    def teardown(self):
        print("TestDemo teardown")

    def test_demo1(self):
        print("test demo1")

    def test_demo2(self):
        print("test demo2")

    def test_demo3(self):
        print("test demo3")


class TestDemo1:

    #  执行类 前后分别执行setup_class  teardown_class
    def setup_class(self):
        print("TestDemo setup_class")

    def teardown_class(self):
        print("TestDemo teardown_class")

    # 每个类里面的方法前后分别执行 setup, teardown
    def setup(self):
        print("TestDemo setup")

    def teardown(self):
        print("TestDemo teardown")

    def test_demo1(self):
        print("test demo1")

    def test_demo2(self):
        print("test demo2")

print(__name__)

运行结果:

============================= test session starts ==============================
collecting ... collected 7 items

test_setup_teardown.py::test_case1 资源准备:setup module
资源准备:setup function
PASSED                                [ 14%]case1
资源消毁:teardown function

test_setup_teardown.py::test_case2 资源准备:setup function
PASSED                                [ 28%]case2
资源消毁:teardown function

test_setup_teardown.py::TestDemo::test_demo1 
test_setup_teardown.py::TestDemo::test_demo2 
test_setup_teardown.py::TestDemo::test_demo3 
test_setup_teardown.py::TestDemo1::test_demo1 TestDemo setup_class
TestDemo setup
PASSED                      [ 42%]test demo1
TestDemo teardown
TestDemo setup
PASSED                      [ 57%]test demo2
TestDemo teardown
TestDemo setup
PASSED                      [ 71%]test demo3
TestDemo teardown
TestDemo teardown_class

test_setup_teardown.py::TestDemo1::test_demo2 

======================== 7 passed, 10 warnings in 0.08s ========================

Process finished with exit code 0
TestDemo setup_class
TestDemo setup
PASSED                     [ 85%]test demo1
TestDemo teardown
TestDemo setup
PASSED                     [100%]test demo2
TestDemo teardown
TestDemo teardown_class
资源准备:teardown module