p-unit 教程3 -- 执行参数化测试案例

写性能测试案例的朋友经常会注意到,同一个测试场景经常需要测试不同数量级的参数,p-unit很好的支持这种参数化测试案例。p-unit要求参数化测试案例实现接口Parameterizable,接口的主要函数是parameters(),返回一个Parameter的参数列表。然后p-unit会将该列表中的参数一一传入你的测试函数,当然测试函数的声明为:
public void testA(MyParameter param)
一段实例代码:
public class ParamTestClass implements Parameterizable {

public static void main(String[] args) {
new SoloRunner().run(ParamTestClass.class);
}

public Parameter[] parameters() {
return new Parameter[] { new ParameterImpl(10), new ParameterImpl(20) };
}

public void testA(ParameterImpl param) {
SampleUtil.doSomething();
}

public void testB(ParameterImpl param) {
SampleUtil.doSomething();
}

public void testC(ParameterImpl param) {
SampleUtil.doSomething();
}

public void setUpAfterWatchers(Parameter param) throws Exception {

}

public void setUpBeforeWatchers(Parameter param) throws Exception {

}

public void tearDownAfterWatchers(Parameter param) throws Exception {

}

public void tearDownBeforeWatchers(Parameter param) throws Exception {

}

static class ParameterImpl implements Parameter {
private int _count;

ParameterImpl(int count) {
_count = count;
}

public int count() {
return _count;
}

public String toString() {
return String.valueOf(_count);
}
}
}

运行结果为:
[solo] Started running samples.ParamTestClass
samples.ParamTestClass
testA(10) - [49584.0bytes,363.0ms]
testA(20) - [25680.0bytes,244.0ms]
testB(10) - [90760.0bytes,349.0ms]
testB(20) - [34640.0bytes,32.0ms]
testC(10) - [19296.0bytes,75.0ms]
testC(20) - [0.0bytes,230.0ms]
total: 6, failures:0 (GREEN) 2230.0ms

是不是很简单?这就是p-unit的设计理念,下一节将会讲述如何测试不同运行环境的性能。

小tip: 建议重载你的参数的toString函数,实例中的ParameterImpl#toString,他将现实在运行结果或是运行报表中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值