<5>pytest:前置后置

2021/12/5 6:17:26

本文主要是介绍<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


这篇关于<5>pytest:前置后置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程