pytest测试框架 -- setup和teardown等

一、用例运行级别

1、函数级别(setup、teardown 或 setup_function、teardown_function):

仅对处于同作用域的测试函数有效(该函数定义不在类中,则对非类中测试函数有效;若该函数定义在类中,则对类中测试函数有效)

def setup_function():        #  setup()也一样
    print("setup_function")

def teardown_function():        #  teardown()也一样 
    print("teardown_function")

def test_01():
    print("---用例a执行---")

class TestCase():
    def test_02(self):
        print("---用例b执行---")

    def test_03(self):
        print("---用例c执行---")

def test_04():
    print("---用例d执行---")

输出结果:
test_fixture2.py setup_function
---用例a执行---
.teardown_function
---用例b执行---
.---用例c执行---
.setup_function
---用例d执行---
.teardown_functio

2、方法级别(setup_method、teardown_method):

该函数只能定义在类中,且对类中的测试函数均有效

def test_01():
    print("---用例a执行---")

class TestCase():
    def setup_method(self):
        print("setup_method")

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

    def test_02(self):
        print("---用例b执行---")

    def test_03(self):
        print("---用例c执行---")

输出结果:
test_fixture2.py ---用例a执行---
.setup_method
---用例b执行---
.teardown_method
setup_method
---用例c执行---
.teardown_method

3、类级别(setup_class、teardown_class):

该函数只能定义在类中,且分别在类开始和结束前各执行一次

def test_01():
    print("---用例a执行---")

class TestCase():
    def setup_class(self):
        print("setup_class")

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

    def test_02(self):
        print("---用例b执行---")

    def test_03(self):
        print("---用例c执行---")

输出结果:
test_fixture2.py ---用例a执行---
.setup_class
---用例b执行---
.---用例c执行---
.teardown_class

4、模块级别(setup_module、teardown_module):

该函数只可定义在非类中,且分别在所有测试函数开始和结束前各执行一次

def setup_module():
    print("setup_module")

def teardown_module():
    print("teardown_module")

def test_01():
    print("---用例a执行---")

class TestCase():

    def test_02(self):
        print("---用例b执行---")

    def test_03(self):
        print("---用例c执行---")

def test_04():
    print("---用例d执行---")

输出结果:
setup_module
---用例a执行---
.---用例b执行---
.---用例c执行---
.---用例d执行---
.teardown_modul

5、不同级别函数混合:

运行的优先级:module > function > class > method > setup、teardown

def setup_module():
    print("setup_module")

def teardown_module():
    print("teardown_module")

def setup_function():
    print("setup_function")

def teardown_function():
    print("teardown_function")

def test_01():
    print("---用例a执行---")

class TestCase():

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

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

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

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

    def setup_class(self):
        print("setup_class")

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

    def test_02(self):
        print("---用例b执行---")

    def test_03(self):
        print("---用例c执行---")

输出结果:
test_fixture2.py setup_module
setup_function
---用例a执行---
.teardown_function
setup_class
setup_method
setup
---用例b执行---
.teardown
teardown_method
setup_method
setup
---用例c执行---
.teardown
teardown_method
teardown_class
teardown_module

 

参考:https://www.cnblogs.com/yoyoketang/p/9374957.html

posted @ 2020-07-29 14:48  Maruying  阅读(210)  评论(0编辑  收藏  举报