unittest 用于测试一个源代码单元, 官方文档
重复一次以上的代码,就要自动化
unittest 使用
- 编写源代码
源代码文件名, Person.py
class Person:
name = []
def set_name(self, user_name):
self.name.append(user_name)
return len(self.name) - 1
def get_name(self, user_id):
if user_id >= len(self.name):
return 'There is no such user'
else:
return self.name[user_id]
if __name__ == "__main__":
person = Person()
print('User Abbas has been added with id ', person.set_name('Abbas'))
print('User associated with id 0 is ', person.get_name(0))
- 编写单元测试
单元测试文件名 Person_test.py
import unittest
# 这是我们想要测试的类,所以需要导入它
import Person as PersonClass
class Test(unittest.TestCase):
"""
基数继承自 unittest.TestCase
"""
person = PersonClass.Person() # 实例化 Person class
user_id = [] # 存储 userid
user_name = [] # 存储 user name
# 测试用例检查 Person.set_name 功能
def test_0_set_name(self):
"""
任何 'test_' 开头的方法,被认为一个测试用例
"""
print("start set_name test \n")
for i in range(4):
# 初始化一个name
name = 'name' + str(i)
# 保存 name 到 user_name list
self.user_name.append(name)
# 获取 user id
user_id = self.person.set_name(name)
# 检查获得的 user id 为空,或者不是
self.assertIsNotNone(user_id)
# 保存 user id 到 user_id list
self.user_id.append(user_id)
print('user_id length = ', len(self.user_id))
print(self.user_id)
print('user_name length =', len(self.user_name))
print(self.user_name)
print('\nFinish set_name test\n')
# 测试用例检查 Person.get_name 功能
def test_1_get_name(self):
print("\nstart get_name test\n")
"""
任何 'test_' 开头的方法,被认为一个测试用例
"""
length = len(self.user_id) # 存储的用户信息的总数
print('user_id length = ', length)
print('user_name length = ', len(self.user_name))
for i in range(6):
"""
测试用例完整的检查 Person.get_name的各逻辑分支
"""
if i < length:
# 验证逻辑: 如果不超过总数,验证返回的 用户名
# 如果两个名称不匹配,测试用例结果:失败
self.assertEqual(self.user_name[i],
self.person.get_name(self.user_id[i]))
else:
print('Testing for get_name no user test')
# 超过总数的,再做 'no such user' 类型检查
self.assertEqual('There is no such user',
self.person.get_name(i))
print('\nFinish get_name test\n')
if __name__ == "__main__":
unittest.main()
请注意,unittest 模块按其名称的顺序执行测试功能(不是按定义的顺序执行)
我们希望首先执行 set_name
,所以我们将测试用例函数命名为 test_0_set_name
和 test_1_get_name
- 运行单元测试
python -m unittest Person_test.Test
- 测试结果
D:\projects\pythoncode\pythonstudy>python -m unittest Person_test.Test
start set_name test
user_id length = 4
[0, 1, 2, 3]
user_name length = 4
['name0', 'name1', 'name2', 'name3']
Finish set_name test
.
start get_name test
user_id length = 4
user_name length = 4
Testing for get_name no user test
Testing for get_name no user test
Finish get_name test
.
----------------------------------------------------------------------
Ran 2 tests in 0.002s
OK
详细输出
D:\projects\pythoncode\pythonstudy>python -m unittest -v Person_test.Test
test_0_set_name (Person_test.Test) ... start set_name test
user_id length = 4
[0, 1, 2, 3]
user_name length = 4
['name0', 'name1', 'name2', 'name3']
Finish set_name test
ok
test_1_get_name (Person_test.Test) ...
start get_name test
user_id length = 4
user_name length = 4
Testing for get_name no user test
Testing for get_name no user test
Finish get_name test
ok
----------------------------------------------------------------------
Ran 2 tests in 0.005s
OK
D:\projects\pythoncode\pythonstudy>
测试步骤
单元测试结构
基本结构
通过子类化 unittest.TestCase
来创建单个单元测试用例
import unittest
class Testing(unittest.TestCase):
def test_string(self):
a = 'some'
b = 'some'
self.assertEqual(a, b)
def test_boolean(self):
a = True
# b = True
b = False
self.assertEqual(a, b)
if __name__ == "__main__":
unittest.main()
运行方式
两种方式