Junit多参数测试

日前,为测试一个传入多参数的方法,使用了junit进行单元测试,由于接触单元测试不久,很多不熟悉的地方,弄了挺久的。

这里试用了两种方法进行测试,由于是新手,很多需要改善学习。

示例方法:

public void add(Long a,Long b,String c,String d,Date date1,Date 2){...}

 假设a和b的取值范围为{-1,0,1,2},c和d的取值为{null,"123","sdfsdf"},data1和data2的取值为{null,new Data(),new Date(new Date.getTime()-24*3600),new Date(new Date.getTime()+24*3600)}

为了覆盖每一种情况需要每一个参数取一种可能组合,共有4*4*3*3*4*4=2304种可能,需要遍历 

1、参数化测试 

JUnit参数化测试的五个步骤:
(1)为准备使用参数化测试的测试类指定特殊的运行器 org.junit.runners.Parameterized。
(2)为测试类声明几个变量,分别用于存放期望值和测试所用数据。
(3)为测试类声明一个带有参数的公共构造函数,并在其中为第二个环节中声明的几个变量赋值。
(4)为测试类声明一个使用注解 org.junit.runners.Parameterized.Parameters 修饰的,返回值为 java.util.Collection 的公共静态方法,
并在此方法中初始化所有需要测试的参数对。
(5)编写测试方法,使用定义的变量作为参数进行测试。 

import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class parTest {

    Long a;
    Long b;
    String c;
    String d;
    Date date1;
    Date date2;

    // 带参数的构造函数
    public parTest(Long a, Long b, String c, String d, Date date1, Date date2) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.date1 = date1;
        this.date2 = date2;
    }

    @Parameters
    public static Collection<?> prepareData() {
        return Ptest.filled();
    }

    @Test
    public void test() {
        // 调用方法
        add(a, b, c, d, date1, date2);
    }

}

 参数组合: 

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Ptest {

    public static void main(String[] args) {
        filled();
    }

    public static List<Object[]> filled() {

        Long a[] = { -1l, 0l, 1l, 2l };
        Long b[] = { -1l, 0l, 1l, 2l };
        String[] c = { null, "123", "sfsfdsf" };
        String[] d = { null, "123", "sfsfdsf" };
        Date[] date1 = { null, new Date(),
                new Date(new Date().getTime() - 24 * 3600) };
        Date[] date2 = { null, new Date(),
                new Date(new Date().getTime() + 24 * 3600) };
        List<Object[]> resultList = diff(a, b, c, d, date1, date2);
        return resultList;
    }

    public static List<Object[]> diff(Object[]... a) {
        List<List<Object>> f = getFirstList(a[0]);
        for (int i = 1; i < a.length; i++) {
            f = diff(f, a[i]);
        }

        List<Object[]> resultList = new ArrayList<Object[]>();
        for (int i = 0; i < f.size(); i++) {
            Object[] o = new Object[f.get(i).size()];
            for (int j = 0; j < f.get(i).size(); j++) {
                o[j] = f.get(i).get(j);
            }
            resultList.add(o);
        }
        return resultList;
    }

    public static List<List<Object>> getFirstList(Object[] first) {
        List<List<Object>> result = new ArrayList<List<Object>>();
        for (Object i : first) {
            List<Object> b = new ArrayList<Object>();
            b.add(i);
            result.add(b);
        }
        return result;
    }

    public static List<List<Object>> diff(List<List<Object>> array, Object[] a) {
        List<List<Object>> result = new ArrayList<List<Object>>();
        for (List<Object> as : array) {
            for (Object i : a) {
                List<Object> b = new ArrayList<Object>();
                b.addAll(as);
                b.add(i);
                result.add(b);
            }
        }
        return result;
    }
}

2、打包传参 

package Suite;

import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class TestCaseSuit
{

    public static Test suite()
    {
        TestSuite suite = new TestSuite();//创建一个测试套件
        suite.addTest(new JUnit4TestAdapter(TestParam.class));// 增加测试类的class对象
        for (int i = 0; i < 10; i++) {//测试次数
            TestParam tp = new TestParam("add");//构造时指定测试方法,执行一次默认参数的测试
            tp.setParam(a, b, c, d, date1, date2);//测试入参
            suite.addTest(tp);//执行测试入参的测试
        }
        return suite;
    }
    
}

 

package Suite;

import java.util.ArrayList;
import java.util.Date;

import org.junit.Test;

import junit.framework.TestCase;

public class TestParam extends TestCase {

    
    // 测试的数据参数
    Long a;
    Long b;
    String c;
    String d;
    Date date1;
    Date date2;

    // 带参数的构造函数
    public parTest(Long a, Long b, String c, String d, Date date1, Date date2) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.date1 = date1;
        this.date2 = date2;
    }
    public TestParam(String name) {
        // TODO Auto-generated constructor stub
        super(name);
    }

    @Test
    public void test() {
        // 调用方法
        add(a, b, c, d, date1, date2);
    }
}

 

posted @ 2014-08-19 22:38  Sheiloo  阅读(1168)  评论(0编辑  收藏  举报