JUnit从4.4版本开始引入理论机制,它使得开发人员从开始设计测试用例的时候,就可以通过参数集(理论上是无限个参数)对被测对象进行概括性的描述,通过将构造好的参数集遍历传入每个case,实现对测试对象的覆盖。一个简单的例子如下:
import static org.junit.Assert.*;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
@RunWith(Theories.class)
public class TestBoolean {
@DataPoint
public static boolean paramA = true;
@DataPoint
public static boolean paramB = false;
@Theory
public void testCase(boolean param) {
assertTrue(param);
}
}
代码中声明的两个参数paramA、paramB,会作为数据源轮流传入用例中,用例通过形参过滤数据源。假如还声明了int类型数据源,则不会传入testCase中,因为它只接受boolean类型参数。假如testCase需要使用多个数据源,则可以在参数列表中扩展,如testCase(boolean parama, boolean paramb),paramA、paramB会遍历传入testCase中,即会有四种组合。当然,也可以设定为不同类型形参。
使用要点如下:
1、指定运行runner为Theories.class
2、参数集用@DataPoint注解,且必须是public static类型
3、具体用例不使用@Test注解,而是@Theory注解
不过遗憾的是,eclipse对JUnit理论机制支持并不好,如上面的testCase,case运行到第二次时,肯定会失败,但eclipse不是提示失败,而是直接抛出异常,且通过JUnit窗口点击进入代码时,会弹出警告窗口:
JUnit理论机制的另一个缺陷是数据源采用硬编码,无法通过配置文件动态配置。
鉴于理论机制的上述不足,建议对于有不同数据源的需求,可以直接使用JUnit的参数化测试,类似的例子如下:
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
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 TestBoolean {
private boolean param;
@SuppressWarnings("rawtypes")
@Parameters
public static Collection data() {
return Arrays.asList(new Object[][] { { true },
{ false } });
}
public TestBoolean(boolean param) {
this.param = param;
}
@Test
public void testCase() {
assertTrue(param);
}
}
与理论机制类似,参数化测试将数据源放入一个静态容器中,容器中有几组参数,用例就会运行几次,且每次会以不同参数执行。在用例失败时,也会给出详细失败信息,在JUnit窗口中可直接定位:
使用要点如下:
1、指定运行runner为Parameterized.class
2、参数集用放入一个返回类型Collection的容器中,用@Parameters注解,且必须是public static类型
3、需要声明相应类型的变量,在构造方法中取回指定参数
4、具体用例使用@Test注解,且不需要传入形参
5、参数集是个二维数组,第一维指定了总共有几组参数(即case要运行多少遍),第二维指定了每组参数有几个(本文只使用了boolean型一个参数,有的case可能需要多个参数)
除了运行比较直观,参数集还可通过配置文件动态配置,构造只含一个参数的代码如下(假设配置文件是一个字符串,数据间用逗号分隔):
@Parameters
public static Collection data() {
String strTemp = property.getValue("params");
String[] arrayTemp = strTemp.split("\\,");
if (arrayTemp.length > 0) {
List arrayList = Arrays.asList(arrayTemp);
Object[][] tt = new Object[arrayList.size()][1];
for (int i = 0; i < arrayList.size(); i++) {
tt[i][0] = arrayList.get(i);
}
return Arrays.asList(tt);
} else {
fail("params in configure file is null!");
return null;
}
}