pytest setup和teardown简介

模块级(setup_module/teardown_module) 开始于模块始末。全局的
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后

import pytest
class TestCase01(object):
    @classmethod
    def setup_class(cls):
        print('setup_class')

    @classmethod
    def teardown_class(cls):
        print('teardown_class')
    def setup_method(self):
        print('setup_method')
    def teardown_method(self):
        print('teardown_method')
    def setup(self):
        print('setup')
    def teardown(self):
        print('teardown')
    def test1(self):
        print('test1')

    def test2(self):
        print('test2')

def setup_function():
    print('setup_function')
def teardown_function():
    print('teardown_function')

def setup_module():
    print('setup_module')
def teardown_module():
    print('teardown_module')

def test1():
    print('test1')
def test2():
    print('test2')
if __name__ == '__main__':
    pytest.main(['test07.py','-sv'])
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容