介绍

在自动化测试过程中,经常需要进行初始化和后期处理等工作,例如用例执行前环境配置,或者用例执行完毕清除配置等,这个时候我们就需要用到 setup/teardown 前后置。

  • setup的作用:用来实现执行前的一些初始化操作(如:数据准备、连接设备、打开APP/浏览器、创建日志对象、创建数据库连接、创建接口的请求对象等操作);

  • teardown的作用:用来实现执行用例后的一些操作(如:数据清理、关闭APP/浏览器、销毁日志对象、关闭数据库连接等操作)。

Pytest提供了5种类型的setup 和 teardown 的方法,具体如下:

  • 模块级别: setup_moduleteardown_module
  • 函数级别: setup_functionteardown_function
  • 类级别: setup_classteardown_class
  • 方法级别: setup_methodteardown_methond
  • 默认级别:setupteardown

一、模块级别

模块级别是全局的,在模块运行前执行一次 setup_module() 方法,在模块运行后执行一次 teardown_module() 方法。

示例:创建 test_st.py 文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def setup_module():
print("[模块前置]")

def teardown_module():
print('[模块后置]')

class Test1:
def test1_a(self):
print('这是类方法 test1_a')

def test1_b(self):
print('这是类方法 test1_b')

def test2():
print('这是函数 test2')

运行结果如下:

1
2
3
4
5
6
7
test_st.py::Test1::test1_a [模块前置]
这是类方法 test1_a
PASSED
test_st.py::Test1::test1_b 这是类方法 test1_b
PASSED
test_st.py::test2 这是函数 test2
PASSED[模块后置]

由以上输出结果可知,setup_module/teardown_module 作用于模块级别的前后置,每个模块执行一次。

二、函数级别

函数级别前后置,只对函数用例生效,在每个函数用例前后执行,不包括类中方法。

示例:创建 test_st.py 文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def setup_function():
print("[函数前置]")

def teardown_function():
print('[函数后置]')

class Test1:
def test1_a(self):
print('这是类方法 test1_a')

def test1_b(self):
print('这是类方法 test1_b')

def test2():
print('这是函数 test2')

运行结果如下:

1
2
3
4
5
6
7
test_st.py::Test1::test1_a 这是类方法 test1_a
PASSED
test_st.py::Test1::test1_b 这是类方法 test1_b
PASSED
test_st.py::test2 [函数前置]
这是函数 test2
PASSED[函数后置]

由以上输出结果可知,setup_function/teardown_function 作用于函数级别的前后置,每个函数执行一次。

三、类级别

类级别前后置,只对类用例生效,在类的全部方法前执行一次 setup_class() ,全部方法后执行一次 teardown_class()

示例:创建 test_st.py 文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Test1:
def setup_class(self):
print("[类前置]")

def teardown_class(self):
print('[类后置]')

def test1_a(self):
print('这是类方法 test1_a')

def test1_b(self):
print('这是类方法 test1_b')


def test2():
print('这是函数 test2')

运行结果如下:

1
2
3
4
5
6
7
8
test_st.py::Test1::test1_a [类前置]
这是类方法 test1_a
PASSED
test_st.py::Test1::test1_b 这是类方法 test1_b
PASSED[类后置]

test_st.py::test2 这是函数 test2
PASSED

由以上输出结果可知,setup_class/teardown_class 作用于类级别的前后置,每个类执行一次。

四、方法级别

类方法级别前后置,只对类方法用例生效,在每个类方法用例前执行 setup_method() ,执行后再执行一次 teardown_method()

示例:创建 test_st.py 文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Test1:
def setup_method(self):
print("[类方法前置]")

def teardown_method(self):
print('[类方法后置]')

def test1_a(self):
print('这是类方法 test1_a')

def test1_b(self):
print('这是类方法 test1_b')


def test2():
print('这是函数 test2')

运行结果如下:

1
2
3
4
5
6
7
8
9
10
test_st.py::Test1::test1_a [类方法前置]
这是类方法 test1_a
PASSED[类方法后置]

test_st.py::Test1::test1_b [类方法前置]
这是类方法 test1_b
PASSED[类方法后置]

test_st.py::test2 这是函数 test2
PASSED

由以上输出结果可知,setup_method/teardown_method 作用于方法级别的前后置,每个类方法执行一次。

五、默认级别

示例:创建 test_st.py 文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def setup():
print("[前置]")


def teardown():
print("[后置]")


class Test1:

def test1_a(self):
print('这是类方法 test1_a')

def test1_b(self):
print('这是类方法 test1_b')


def test2():
print('这是函数 test2')

运行结果如下:

1
2
3
4
5
6
7
8
9
test_st.py::Test1::test1_a [前置]
这是类方法 test1_a
PASSED
test_st.py::Test1::test1_b 这是类方法 test1_b
PASSED
test_st.py::test2 这是函数 test2
PASSED
test_st.py::test3 这是函数 test3
PASSED[后置]

由以上输出结果可知,setup/teardown 作用于全局级别的前后置。