写测试用例的方法
儿童编程
仓库地址
$ git clone git@git.haorenao.cn:api_test.git
创建一个测试模块
模块的名字对应要测试的接口所在模块名字一致
$ mkdir userinfo $ touch userinfo/__init__.py
模块内所有测试文件都以 _test.py 为后缀
$ vim userinfo/example_test.py
创建测试用例类
#! -*- coding: utf-8 -*-from utils.test_tool import TestToolfrom utils.test_tool import AuthTestCasefrom utils.test_tool import NormalTestCaseclass WhoAmITest(AuthTestCase): def test_whoami(self): self.client.set_uri('/server/userinfo/whoami/') data = self.client.request('get') self.assertIsNotNone(data, None) self.assertEquals(data.get('name'), u'小明') ....clase ExampleTest(NormalTestCase): def test_example(self): self.client.set_uri('/server/example/example/') data = self.client.request('get') self.assertIsNotNone(data, None) self.assertTrue(data.get('enable')) ....
| 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
{ "entry": [ { "name": "example", "pattern": "*_test*.py" } ]}
Entry
| 名字 |
描述 |
备注 |
| name |
模块名字 |
|
| pattern |
测试文件查找pattern |
默认:*_test*.py |
运行测试
$ python start.py
测试结果
- 结果是 mardown格式保存在文件中
- 文件名: (日期和时间):
- 测试用例语法错误或代码错误会直接输出,不保存在结果里
- 示例
| 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 |
错误消息 |
|