其他分享
首页 > 其他分享> > <5>pytest:前置后置

<5>pytest:前置后置

作者:互联网

总览

执行顺序

类外

setup_moudle
setup_function
case
teardown_function
teardown_moudle

类中

setup_moudle
setup_class
setup_method
setup
case
teardown
teardown_method
teardown_class
teardown_moudle

代码举例

代码

def setup_module():
    print("\nsetup_module")


def teardown_module():
    print("teardown_module")


def setup_function():
    print("\nsetup_function")


def teardown_function():
    print("teardown_function")


def test_case1():
    print("测试用例_函数1")


def test_case2():
    print("测试用例_函数2")


class TestClass:
    def setup_class(self):
        print("\nsetup_class")

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

    def setup_method(self):
        print("\nsetup_method")

    def teardown_method(self):
        print("teardown_method")

    def setup(self):
        print("setup")

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

    def test_class_case1(self):
        print("测试用例_类_方法1")

    def test_class_case2(self):
        print("测试用例_类_方法2")

输出

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

setup_and_teardown.py::test_case1 
setup_module

setup_function
PASSED                                 [ 25%]测试用例_函数1
teardown_function

setup_and_teardown.py::test_case2 
setup_function
PASSED                                 [ 50%]测试用例_函数2
teardown_function

setup_and_teardown.py::TestClass::test_class_case1 
setup_class

setup_method
setup
PASSED                [ 75%]测试用例_类_方法1
teardown
teardown_method

setup_and_teardown.py::TestClass::test_class_case2 
setup_method
setup
PASSED                [100%]测试用例_类_方法2
teardown
teardown_method
teardown_class
teardown_module


============================== 4 passed in 0.03s ==============================

进程已结束,退出代码为 0

标签:后置,teardown,setup,前置,method,pytest,print,class,def
来源: https://www.cnblogs.com/m1031478472/p/15643967.html