[关闭]
@fuyb1986 2018-11-08T03:55:04.000000Z 字数 1723 阅读 668

写测试用例的方法

儿童编程


仓库地址

  1. $ git clone git@git.haorenao.cn:api_test.git

创建一个测试模块

模块的名字对应要测试的接口所在模块名字一致

  1. $ mkdir userinfo
  2. $ touch userinfo/__init__.py

模块内所有测试文件都以 _test.py 为后缀

  1. $ vim userinfo/example_test.py

创建测试用例类

  1. #! -*- coding: utf-8 -*-
  2. from utils.test_tool import TestTool
  3. from utils.test_tool import AuthTestCase
  4. from utils.test_tool import NormalTestCase
  5. class WhoAmITest(AuthTestCase):
  6. def test_whoami(self):
  7. self.client.set_uri('/server/userinfo/whoami/')
  8. data = self.client.request('get')
  9. self.assertIsNotNone(data, None)
  10. self.assertEquals(data.get('name'), u'小明')
  11. ....
  12. clase ExampleTest(NormalTestCase):
  13. def test_example(self):
  14. self.client.set_uri('/server/example/example/')
  15. data = self.client.request('get')
  16. self.assertIsNotNone(data, None)
  17. self.assertTrue(data.get('enable'))
  18. ....
Method Checks that New in
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b 2.7
assertIsNot(a, b) a is not b 2.7
assertIsNone(x) x is None 2.7
assertIsNotNone(x) x is not None 2.7
assertIn(a, b) a in b 2.7
assertNotIn(a, b) a not in b 2.7
assertIsInstance(a, b) isinstance(a, b) 2.7
assertNotIsInstance(a, b) not isinstance(a, b) 2.7

添加测试模块到 entry.json

  1. {
  2. "entry": [ {
  3. "name": "example",
  4. "pattern": "*_test*.py"
  5. }
  6. ]
  7. }

Entry

名字 描述 备注
name 模块名字
pattern 测试文件查找pattern 默认:*_test*.py

运行测试

  1. $ python start.py

测试结果

Type Uri Test Id Message
FAIL /server/example/example/ example_test.ExampleTest.test_a 1 != 2
FAIL /server/userinfo/whoami/ example_test.ExampleTest.test_whoami None != 3

测试结果格式说明

名字 描述 备注
Type 错误类型
Uri 接口地址
Message 错误消息
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注