一、简介
主要是为了做资源冗余处理,比如说再setup里连接数据库或者打开一些文件之类的,在用完后teardown再释放掉
二、类里边或者函数运行在调用方法前后
一般常用的是这两个作用域方式
import pytest
def setup():
print('setup')
def teardown():
print('teardown')
def test_01():
print('01')
def test_02():
print('02')
class TestCase:
def setup(self):
print('setup')
def teardown(self):
print('teardown')
def test_one(self):
print('one')
def test_two(self):
print('two')
if __name__ == '__main__':
pytest.main(['-s','-v','test_setup.py'])
三、其他作用域使用介绍
- 方法/函数级别(setup/teardown)
- 函数级(setup_function/teardown_function)
- 方法级(setup_method/teardown_method)
- 类级(setup_class/teardown_class)
- 模块级(setup_module/teardown_module)
这里不为大家一一介绍了,后续几种大家可以自行敲写做感受