测试与实验
“单一变量法”,咋这么熟悉的名字?
高中生物课、化学、物理,大学时代的各种实验课中都有提及。
就是这个“单一变量法”,也可用于软件测试领域。
示例
doSomething(req)表示为接口测试过程中的某个方法,Java示例代码如下:
public DoSomethingRes doSomething(DoSomethingReq req) {
// check params
check(req);
DoSomethingRes res= null;
SomeRpc rpc = getSomeRpc();
SomeRpcReq rpcReq = build(req);
rpc.doRpcMethod(rpcReq);
// do something else
return res;
}
请求参数:
字段名称 | 类型 | 取值范围 | 必填项 | 备注 |
---|---|---|---|---|
user | String | 是 | ||
type | int | [0,1,2] | 是 | |
queryStartDate | String | 是 | 格式为yyyy-MM-dd |
返回结果:
字段名称 | 类型 | 备注 |
---|---|---|
success | Bool | true表示成功,false表示失败 |
message | String | |
data | Array | 对象数组 |
示例数据:
// 请求参数
{
"user": "String",
"type": "1",
"queryStartDate": "2018-09-30"
}
// 返回结果
{
"success": true,
"message": "ok",
"data":[
{
...
},
{
...
}
]
}
单一变量法实践如下
选择user、type、queryStartDate三个正确参数,保持其中2个参数不变,仅修改其中1个参数。
(1)检查各个参数进行非空校验的测试用例
- user=null,type=1,queryStartDate=2018-09-30
- user=helloworld,type=null,queryStartDate=2018-09-30
- user=helloworld,type=1,queryStartDate=null
(2)检查各个参数进行空字符串校验的测试用例
- user=空字符串,type=1,queryStartDate=2018-09-30
- user=helloworld,type=空字符串,queryStartDate=2018-09-30
- user=helloworld,type=1,queryStartDate=空字符串
(3)保持user、queryStartDate不变,遍历type枚举项的测试用例
- user=helloworld,type=0,queryStartDate=2018-09-30
- user=helloworld,type=1,queryStartDate=2018-09-30
- user=helloworld,type=2,queryStartDate=2018-09-30
(4)补充测试用例
单一变量法检查接口参数是否正常,可能会导致部分组合参数不能覆盖,需要在测试过程中根据实际代码覆盖率情况补充测试用例。
举例:doSomething内部针对特殊用户,代码执行了不同的业务代码,此时需要对特殊用户增加测试用例。
- user=admin(特殊权限用户),type=0,queryStartDate=2018-09-30
- user=admin(特殊权限用户),type=1,queryStartDate=2018-09-30
- user=admin(特殊权限用户),type=2,queryStartDate=2018-09-30
为何使用单一变量法进行接口测试?
使用单一变量法,可以方便组织测试用例,甚至批量生成用例,并快速进行接口自动化测试。
代码实践
由你来发挥,建议动手实践。
申明
本文由作者同步发布到segmentfault、知乎社区、testerhome。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。