setup 与 teardown


# setup 与 teardown

# 前言

学过 Unittest 的都知道有 2 个前置方法和 2 个后置方法

  • setup()
  • teardown()
  • setupClass()
  • teardownClass()

Pytest 框架也有类似于 setup 和 teardown 的语法:

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

# 示例

import pytest

def setup_module():
    print("setup_module:整个.py模块只执行一次,比如:所有用例开始前只打开一次浏览器")

def teardown_module():
    print("teardown_module:整个.py模块只执行一次,比如:所有用例结束只最后关闭浏览器")

def setup():
    print("setup:每个用例执行前都会执行")

def teardown():
    print("teardown:每个用例结束后都会执行")

def setup_function():
    print("setup_function:每个用例开始前都会执行")

def teardown_function():
    print("teardown_function:每个用例结束前都会执行")

def test_one():
    print("正在执行----test_one")


def test_two():
    print("正在执行----test_two")


class TestCase():

    def setup_class(self):
        print("setup_class:整个测试类所有用例执行之前")

    def teardown_class(self):
        print("teardown_class:整个测试类所有用例执行之后")

    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 test_three(self):
        print("正在执行----test_three")

    def test_four(self):
        print("正在执行----test_four")

if __name__ == "__main__":
    pytest.main(["-s","-q","-ra", "sample.py"])


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

测试结果:

pytest

注释掉以下内容,重新测试:

# def setup_function():
#     print("setup_function:每个用例开始前都会执行")
#
# def teardown_function():
#     print("teardown_function:每个用例结束前都会执行")


 # def setup_method(self):
    #     print("setup_method:类里面每个用例执行前都会执行")
    #
    # def teardown_method(self):
    #     print("teardown_method:类里面每个用例结束后都会执行")
1
2
3
4
5
6
7
8
9
10
11
12

测试结果:

pytest

def setup_module():
    global user_data
    user_data = {"path":"admin","name":"admin"}
    print(id(user_data))
    print("使用前的{}".format(user_data))


def teardown_module():
    global user_data
    user_data = 123
    print("结束后的{}".format(user_data))

def test_003():
    print(user_data)
    print(id(user_data))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

结果:

test_fixture.py::test_003 1920498803200
使用前的{'path': 'admin', 'name': 'admin'}
{'path': 'admin', 'name': 'admin'}
1920498803200
PASSED结束后的123
1
2
3
4
5

总结

  • setup_method/teardown_methodsetup/teardown 都可以作用于测试类中的方法
  • setup_method/teardown_method 的优先级高于 setup/teardown
  • setup 这种设置全局变量后,在测试用例中可以被访问

(完)