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

来源:互联网 发布:电影源码是什么 编辑:程序博客网 时间:2024/06/05 11:22
p-unit最新介绍已在developerWorks发表,请点击这里查阅

写性能测试案例的朋友经常会注意到,同一个测试场景经常需要测试不同数量级的参数,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,他将现实在运行结果或是运行报表中。
原创粉丝点击