• pytest的setup和teardown


    用例运行级别
    • 函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
    • 类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
    • 方法级(setup_method/teardown_method)开始于方法始末(在类中)
    • 类里面的(setup/teardown)运行在调用方法的
    • 模块级(setup_module/teardown_module)开始于模块始末,全局的
    1、setup_method和teardown_method,setup和setdown,二者作用一样,选择其一即可。
     
    一、函数级的(setup_function、teardown_function)只对函数用例生效,而且不在类中使用
    #!/usr/bin/env/python
    # -*-coding:utf-8-*-
    
    import pytest
    
    """
    只对函数用例生效,不在类中
    setup_function
    teardown_function
    """
    
    
    def setup_function():
        print("setup_function():每个方法之前执行")
    
    
    def teardown_function():
        print("teardown_function():每个方法之后执行")
    
    
    def test_01():
        print("正在执行test1*****")
        x = "this"
        assert 'h' in x
    
    
    def test_02():
        print("正在执行test2*******")
        x = "hello"
        assert 'h' in x
    
    
    def add(a,b):
        return a+b
    
    
    def test_add():
        print("正在执行test_add()*******")
        assert add(3,4) == 7
    
    if __name__=="__main__":
        pytest.main(["-s","test_time.py"])

    运行结果:

    二、类级的(setup_class、teardown_class)在类中使用,类执行之前运行一次,类执行之后运行一次

    #!/usr/bin/env/python
    # -*-coding:utf-8-*-
    
    import pytest
    
    """
    只对函数用例生效,不在类中
    setup_function
    teardown_function
    """
    
    class TestClass(object):
        def setup_class(self):
            print("setup_function():每个方法之前执行")
    
    
        def teardown_class(self):
            print("teardown_function():每个方法之后执行")
    
    
        def test_01(self):
            print("正在执行test1*****")
            x = "this"
            assert 'h' in x
    
    
        def test_02(self):
            print("正在执行test2*******")
            x = "hello"
            assert 'h' in x
    
    
        def add(self,a,b):
            return a+b
    
    
        def test_add(self):
            print("正在执行test_add()*******")
            assert self.add(3,4) == 7
    
    if __name__=="__main__":
        pytest.main(["-s","test_time.py"])

    运行结果:

    三、类中方法级的(setup_method、teardown_method)在每一个方法之前执行一次,在每一个方法之后执行一次

    #!/usr/bin/env/python
    # -*-coding:utf-8-*-
    
    import pytest
    
    """
    只对函数用例生效,不在类中
    setup_function
    teardown_function
    """
    
    class TestClass(object):
        def setup_class(self):
            print("setup_class(self):每个类之前执行一次
    ")
    
        def teardown_class(self):
            print("teardown_class(self):每个类之后执行一次")
    
        def setup_method(self):
            print("setup_method(self):在每个方法之前执行")
    
        def teardown_method(self):
            print("teardown_method(self):在每个方法之后执行
    ")
    
    
        def test_01(self):
            print("正在执行test1*****")
            x = "this"
            assert 'h' in x
    
    
        def test_02(self):
            print("正在执行test2*******")
            x = "hello"
            assert 'h' in x
    
    
        def add(self,a,b):
            return a+b
    
    
        def test_add(self):
            print("正在执行test_add()*******")
            assert self.add(3,4) == 7
    
    if __name__=="__main__":
        pytest.main(["-s","test_time.py"])

    运行结果:

    四、模块级的(setup_module、teardown_module)全局的,在模块执行前运行一遍,在模块执行后运行一遍

    #!/usr/bin/env/python
    # -*-coding:utf-8-*-
    
    import pytest
    
    """
    只对函数用例生效,不在类中
    setup_function
    teardown_function
    """
    
    
    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("正在执行test1*****")
        x = "this"
        assert 'h' in x
    
    
    def test_02():
        print("正在执行test2*******")
        x = "hello"
        assert 'h' in x
    
    
    def add(a,b):
        return a+b
    
    
    def test_add():
        print("正在执行test_add()*******")
        assert add(3,4) == 7
    
    if __name__=="__main__":
        pytest.main(["-s","test_time.py"])

    运行结果:

    五、当类和函数都有的时候

    #!/usr/bin/env/python
    # -*-coding:utf-8-*-
    
    """
    在类之前和之后执行一次
    setup_class
    teardown_class
    """
    
    import pytest
    
    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_10():
        print("正在执行test1")
        x = "this"
        assert 'h' in x
    
    def add0(a,b):
        return a+b
    
    def test_add():
        print("正在执行test_add()")
        assert add0(3,4) == 7
    
    class TestClass(object):
    
        def setup_class(self):
            print("setup_class(self):每个类之前执行一次")
    
        def teardown_class(self):
            print("teardown_class(self):每个类之后执行一次")
    
        def add(self,a,b):
            print("这是加法运算")
            return a+b
    
        def test_01(self):
            print("正在执行test1")
            x = "this"
            assert 'h' in x
    
        def test_add(self):
            print("正在执行test_add()")
            assert self.add(3, 4) == 7
    
    if __name__ == '__main__':
        pytest.main(["-s","test_time.py"])

    运行结果:

    顺序:
    setup_modele --> setup_function -->test1 -->teardown_function --> setuo_function -->test_add -->teardown_function -->setup_class -->teardown_class-->taerdown_module
  • 相关阅读:
    2018-08-07
    2018-08-06
    2018-08-03
    安装mysql
    iOS-----------关于组件化
    QQ路径
    iOS----------viewcontroller中的dealloc方法不调用
    【2020Python修炼记】前端开发之 JavaScript 的 BOM 和 DOM 操作
    【2020Python修炼记】前端开发之 JavaScript 内置方法
    【2020Python修炼记】前端开发之 JavaScript 函数
  • 原文地址:https://www.cnblogs.com/guo2733/p/10521803.html
Copyright © 2020-2023  润新知