unittest 用于测试一个源代码单元, 官方文档

重复一次以上的代码,就要自动化

unittest 使用

  1. 编写源代码

源代码文件名, Person.py

  1. class Person:
  2. name = []
  3. def set_name(self, user_name):
  4. self.name.append(user_name)
  5. return len(self.name) - 1
  6. def get_name(self, user_id):
  7. if user_id >= len(self.name):
  8. return 'There is no such user'
  9. else:
  10. return self.name[user_id]
  11. if __name__ == "__main__":
  12. person = Person()
  13. print('User Abbas has been added with id ', person.set_name('Abbas'))
  14. print('User associated with id 0 is ', person.get_name(0))
  1. 编写单元测试

单元测试文件名 Person_test.py

  1. import unittest
  2. # 这是我们想要测试的类,所以需要导入它
  3. import Person as PersonClass
  4. class Test(unittest.TestCase):
  5. """
  6. 基数继承自 unittest.TestCase
  7. """
  8. person = PersonClass.Person() # 实例化 Person class
  9. user_id = [] # 存储 userid
  10. user_name = [] # 存储 user name
  11. # 测试用例检查 Person.set_name 功能
  12. def test_0_set_name(self):
  13. """
  14. 任何 'test_' 开头的方法,被认为一个测试用例
  15. """
  16. print("start set_name test \n")
  17. for i in range(4):
  18. # 初始化一个name
  19. name = 'name' + str(i)
  20. # 保存 name 到 user_name list
  21. self.user_name.append(name)
  22. # 获取 user id
  23. user_id = self.person.set_name(name)
  24. # 检查获得的 user id 为空,或者不是
  25. self.assertIsNotNone(user_id)
  26. # 保存 user id 到 user_id list
  27. self.user_id.append(user_id)
  28. print('user_id length = ', len(self.user_id))
  29. print(self.user_id)
  30. print('user_name length =', len(self.user_name))
  31. print(self.user_name)
  32. print('\nFinish set_name test\n')
  33. # 测试用例检查 Person.get_name 功能
  34. def test_1_get_name(self):
  35. print("\nstart get_name test\n")
  36. """
  37. 任何 'test_' 开头的方法,被认为一个测试用例
  38. """
  39. length = len(self.user_id) # 存储的用户信息的总数
  40. print('user_id length = ', length)
  41. print('user_name length = ', len(self.user_name))
  42. for i in range(6):
  43. """
  44. 测试用例完整的检查 Person.get_name的各逻辑分支
  45. """
  46. if i < length:
  47. # 验证逻辑: 如果不超过总数,验证返回的 用户名
  48. # 如果两个名称不匹配,测试用例结果:失败
  49. self.assertEqual(self.user_name[i],
  50. self.person.get_name(self.user_id[i]))
  51. else:
  52. print('Testing for get_name no user test')
  53. # 超过总数的,再做 'no such user' 类型检查
  54. self.assertEqual('There is no such user',
  55. self.person.get_name(i))
  56. print('\nFinish get_name test\n')
  57. if __name__ == "__main__":
  58. unittest.main()

请注意,unittest 模块按其名称的顺序执行测试功能(不是按定义的顺序执行)
我们希望首先执行 set_name ,所以我们将测试用例函数命名为 test_0_set_nametest_1_get_name

  1. 运行单元测试
  1. python -m unittest Person_test.Test
  1. 测试结果
  1. D:\projects\pythoncode\pythonstudy>python -m unittest Person_test.Test
  2. start set_name test
  3. user_id length = 4
  4. [0, 1, 2, 3]
  5. user_name length = 4
  6. ['name0', 'name1', 'name2', 'name3']
  7. Finish set_name test
  8. .
  9. start get_name test
  10. user_id length = 4
  11. user_name length = 4
  12. Testing for get_name no user test
  13. Testing for get_name no user test
  14. Finish get_name test
  15. .
  16. ----------------------------------------------------------------------
  17. Ran 2 tests in 0.002s
  18. OK

详细输出

  1. D:\projects\pythoncode\pythonstudy>python -m unittest -v Person_test.Test
  2. test_0_set_name (Person_test.Test) ... start set_name test
  3. user_id length = 4
  4. [0, 1, 2, 3]
  5. user_name length = 4
  6. ['name0', 'name1', 'name2', 'name3']
  7. Finish set_name test
  8. ok
  9. test_1_get_name (Person_test.Test) ...
  10. start get_name test
  11. user_id length = 4
  12. user_name length = 4
  13. Testing for get_name no user test
  14. Testing for get_name no user test
  15. Finish get_name test
  16. ok
  17. ----------------------------------------------------------------------
  18. Ran 2 tests in 0.005s
  19. OK
  20. D:\projects\pythoncode\pythonstudy>

测试步骤

image.png

单元测试结构

基本结构
通过子类化 unittest.TestCase 来创建单个单元测试用例

  1. import unittest
  2. class Testing(unittest.TestCase):
  3. def test_string(self):
  4. a = 'some'
  5. b = 'some'
  6. self.assertEqual(a, b)
  7. def test_boolean(self):
  8. a = True
  9. # b = True
  10. b = False
  11. self.assertEqual(a, b)
  12. if __name__ == "__main__":
  13. unittest.main()

运行方式

两种方式
image.png