python单元测试框架unittest学习笔记三--fixture
1、fixture
fixture是对一个测试用例环境的初始化和销毁
可以在测试用例执行之前自动调用指定的函数,在测试泳衣执行之后自动调用指定的函数
2、控制级别
1)方法级
每个方法执行前和执行后都要调用函数,一个类中如果有多个方法则调用多次
2)类级
不管类中有多少个方法,一个类执行前和执行后只调用一次
3)模块级
不管一个模块(即一个py文件)中有多少类,模块执行前后自动调用函数
接下来对每个控制级进行示例解析
2.1 方法级
在testcase,也就是测试用例所在的class中定义方法
def setUp(self)当测试用例执行前,自动被调用
def tearDown(self)当测试用例执行后,自动被调用
如果一个TestCase中有多个测试用例,那么setUp和tearDown方法就会被多次调用
|
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
|
#!/usr/bin/env python# -*- coding:utf-8 -*-# Author:Richard_Kong"""unitest的基本原理: unitest是python自带的测试框架 unitest 是python的标准测试库四大组件: testcase:测试用例,方法命名基于test_开头,测试用例自行排序执行,排序规则A-Z,0-9 testfixture: 设置添置条件 setup ,后置条件 teardown,每个测试用例方法执行前后都要执行者两个方法 testsuit:测试套件,批量执行用例集,套件运行需要结合运行器 htmltestrunner/testtestrunner test runner :运行器,通过test的run方法去调用执行测试用例集unitest的语法规则:用import unitest导入unitest模块定义一个集成unittest.TestCase的测试用例类,如class XXX(unitest.TestCase)即如果定义了则会在每个测试case执行前先执行setup方法,执行完毕后执行teardown方法用例名 必须以test开头,否则unitest不能识别一个测试用例应该只测试一个方面,测试的目的和测试内容应该很明确,主要是调用seertEqual\assertRaises等断言方法来判断程序执行结果和预期值是否相符合调用unnitest.main()启动测试"""import unittestdef sum(a,b): return a+bclass test01(unittest.TestCase): def setUp(self): print("befor test the setUp function 被调用了") def tearDown(self): print("after test the teardown function 被调用了") def test_01(self): print("in function test 01") def test_02(self): print("in function test 02") def test_03(self): print(sum(1,8)) def test_04(self): print(sum(6,9)) def test_05(self): print(sum("aaa",9))if __name__ == '__main__': unittest.main() |
2.2类级
不管类中有多少方法,一个类开始的时候自动调用函数,结束之后自动调用函数
类级别的fixture 一定是类方法
@classmethod
def setUpClass(cls) 类开始的时候自动调用的方法
@classmethod
def tearDownClass(cls)类结束的时候自动调用的方法
(知识点复习:类方法:what are class methods? Class methods are methods that are not bound to an object, but to… a class!
类方法不是绑定到实例上的,是绑定到类上的,也就是如果想要调用类方法,无需对类进行实例化,推荐直接类名.方法名()就可以直接调用;当然实例化后也可以正常调用
其定义规则:
class CLanguage:
#类构造方法,也属于实例方法
def __init__(self):
self.name = "C语言中文网"
self.add = "http://c.biancheng.net"
#下面定义了一个类方法
@classmethod
def info(cls):
print("正在调用类方法",cls)
)
|
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
|
#!/usr/bin/env python# -*- coding:utf-8 -*-# Author:Richard_Kong"""unitest的基本原理: unitest是python自带的测试框架 unitest 是python的标准测试库四大组件: testcase:测试用例,方法命名基于test_开头,测试用例自行排序执行,排序规则A-Z,0-9 testfixture: 设置添置条件 setup ,后置条件 teardown,每个测试用例方法执行前后都要执行者两个方法 testsuit:测试套件,批量执行用例集,套件运行需要结合运行器 htmltestrunner/testtestrunner test runner :运行器,通过test的run方法去调用执行测试用例集unitest的语法规则:用import unitest导入unitest模块定义一个集成unittest.TestCase的测试用例类,如class XXX(unitest.TestCase)即如果定义了则会在每个测试case执行前先执行setup方法,执行完毕后执行teardown方法用例名 必须以test开头,否则unitest不能识别一个测试用例应该只测试一个方面,测试的目的和测试内容应该很明确,主要是调用seertEqual\assertRaises等断言方法来判断程序执行结果和预期值是否相符合调用unnitest.main()启动测试"""import unittestdef sum(a,b): return a+bclass test01(unittest.TestCase): @classmethod def setUpClass(cls) -> None: print("类方法setUpClass 被调用了") @classmethod def tearDownClass(cls) -> None: print("类方法 tearDown 被调用了") def setUp(self): print("befor test the setUp function 被调用了") def tearDown(self): print("after test the teardown function 被调用了") def test_01(self): print("in function test 01") def test_02(self): print("in function test 02") def test_03(self): print(sum(1,8)) def test_04(self): print(sum(6,9)) # def test_05(self): # print(sum("aaa",9))if __name__ == '__main__': unittest.main() |
2.3模块级
不管py文件中有多少个类,以及类中有多少方法,只自动执行一次
def setUpModule() 在py文件开始的时候自动调用
def tearDownModule() 在py文件结束的时候自动调用
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # Author:Richard_Kong
4 """
5 unitest的基本原理:
6 unitest是python自带的测试框架
7 unitest 是python的标准测试库
8 四大组件:
9 testcase:测试用例,方法命名基于test_开头,测试用例自行排序执行,排序规则A-Z,0-9
10 testfixture: 设置添置条件 setup ,后置条件 teardown,每个测试用例方法执行前后都要执行者两个方法
11 testsuit:测试套件,批量执行用例集,套件运行需要结合运行器 htmltestrunner/testtestrunner
12 test runner :运行器,通过test的run方法去调用执行测试用例集
13 unitest的语法规则:
14 用import unitest导入unitest模块
15 定义一个集成unittest.TestCase的测试用例类,如class XXX(unitest.TestCase)
16 即如果定义了则会在每个测试case执行前先执行setup方法,执行完毕后执行teardown方法
17 用例名 必须以test开头,否则unitest不能识别
18 一个测试用例应该只测试一个方面,测试的目的和测试内容应该很明确,主要是调用seertEqual\assertRaises等断言方法来
19 判断程序执行结果和预期值是否相符合
20 调用unnitest.main()启动测试
21 """
22 import unittest
23
24 def sum(a,b):
25 return a+b
26
27
28 def setUpModule():
29 print("setUpModule 函数被调用")
30
31
32 def tearDownModule():
33 print("tearDownModule函数被调用")
34
35
36 class test01(unittest.TestCase):
37 @classmethod
38 def setUpClass(cls) -> None:
39 print("类方法setUpClass 被调用了")
40 @classmethod
41 def tearDownClass(cls) -> None:
42 print("类方法 tearDown 被调用了")
43
44 def setUp(self):
45 print("befor test the setUp function 被调用了")
46
47 def tearDown(self):
48 print("after test the teardown function 被调用了")
49
50 def test_01(self):
51 print("in function test 01")
52
53 def test_02(self):
54 print("in function test 02")
55
56 def test_03(self):
57 print(sum(1,8))
58
59 def test_04(self):
60 print(sum(6,9))
61
62 # def test_05(self):
63 # print(sum("aaa",9))
64
65
66 if __name__ == '__main__':
67 unittest.main()
小结:
setUp,tearDown,每个方法执行开始和完毕后自动调用
setUpClass,tearDownClass 每个类开始和结束的时候自动调用
setUpModule,tearDownModule每个py文件开始和结束的时候调用

浙公网安备 33010602011771号