-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunittest_lib.py
More file actions
67 lines (49 loc) · 2.05 KB
/
unittest_lib.py
File metadata and controls
67 lines (49 loc) · 2.05 KB
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
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-09-12
# @copyright by hoojo @2018
# @changelog Added python3 `unittest lib` TestCase example
# ===============================================================================
# 标题:unittest TestCase
# ===============================================================================
# 描述:使用 unittest TestCase 测试框架测试用例代码
# -------------------------------------------------------------------------------
# -------------------------------------------------------------------------------
# 构建 fabric 网络配置生成工具 TestCase
# -------------------------------------------------------------------------------
import unittest
class TestStringMethods(unittest.TestCase):
def setUp(self):
print('init by setUp...')
def tearDown(self):
print('end by tearDown...')
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
self.assertTrue('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
# 执行所有用例测试
# unittest.main()
# 装载测试用例
test_cases = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
# 使用测试套件并打包测试用例
test_suit = unittest.TestSuite()
test_suit.addTests(test_cases)
# 运行测试套件,并返回测试结果
test_result = unittest.TextTestRunner(verbosity=2).run(test_suit)
# 生成测试报告
print("testsRun:%s" % test_result.testsRun)
print("failures:%s" % len(test_result.failures))
print("errors:%s" % len(test_result.errors))
print("skipped:%s" % len(test_result.skipped))