zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    车标知识学习网页开发,与Flask通过base64展示二进制图片 #华为云·寻找黑马程序员#
    大型情感剧集Selenium:3_元素定位 #华为云·寻找黑马程序员#
    大型情感剧集Selenium:2_options设置 #华为云·寻找黑马程序员#
    【nodejs原理&源码赏析(9)】用node-ssh实现轻量级自动化部署
    大型情感剧集Selenium:1_介绍 #华为云·寻找黑马程序员#
    使用Python开发小说下载器,不再为下载小说而发愁 #华为云·寻找黑马程序员#
    #华为云·寻找黑马程序员#【代码重构之路】如何“消除”if/else
    #华为云·寻找黑马程序员#【代码重构之路】如何优雅的关闭外部资源
    走近深度学习,认识MoXing:初识华为云ModelArts的王牌利器 — MoXing
    开启 J2EE(一)—‘全明星队伍’
  • 原文地址:https://www.cnblogs.com/Maruying/p/13395864.html
Copyright © 2011-2022 走看看