原文地址http://blog.csdn.net/yqj2065/article/details/39967065
参数化测试
正如数组替代int a0,a1,a2一样,测试加法时assertEquals(3.0, h.add(1, 2), 0.1);相当于声明一个变量,要测试100个怎么办。
所以,有了参数化测试,使用一个Collection收集所有的数据——加法时每一次测试需要的几个数据构成一组,n个组构成Collection。
然后按照JUnit的用法要求,写出单元测试类。(偷懒一下,不想写被测试的业务类X了。下面的例子中假设要测试的方法是,判断一个数是否奇数。)
- package myTest.param;
- import java.util.Arrays;
- import java.util.Collection;
- import org.junit.Test;
- import org.junit.runners.Parameterized;
- import org.junit.runners.Parameterized.Parameters;
- import org.junit.runner.RunWith;
- import static org.junit.Assert.assertEquals;
- @RunWith(Parameterized.class)//1.必须
- public class ParametTestUnit {
- private int input;
- private boolean expected;//expected result
- /**
- * 2.public 构造器赋值一组测试数据
- */
- public ParametTestUnit(int input,boolean expected ) {
- this.input = input;
- this.expected = expected;
- }
- /**
- * 3.由@Parameterized.Parameters修饰一个
- * public static Collection xxx()
- */
- @Parameterized.Parameters
- public static Collection data() {
- return Arrays.asList(new Object[][] {
- { 1, true },
- { 3, true },//
- { 6, false },
- { 11, true },
- { 22, false },
- { 23, true }
- });
- }
- /**
- * 4.JUnit循环地使用各组数据
- */
- @Test
- public void testOdd() {
- System.out.println("Parameterized Number is : " + input);
- assertEquals(expected, input%2!=0);
- }
- }
现在这个单元测试类编写完成,但是在BlueJ中不能够直接运行它(不支持?),自己写一个Main好了。
- package myTest.param;
- import org.junit.runner.JUnitCore;
- import org.junit.runner.Result;
- import org.junit.runner.notification.Failure;
- public class Main {
- public static void go() {
- Result result = JUnitCore.runClasses(ParametTestUnit.class);
- for (Failure failure : result.getFailures()) {
- System.out.println(failure.toString());
- }
- System.out.println(result.wasSuccessful());
- }
- }
运行结果:
Parameterized Number is : 1
Parameterized Number is : 3
Parameterized Number is : 6
Parameterized Number is : 11
Parameterized Number is : 22
Parameterized Number is : 23
true
现在,将某个数据修改一下,如{ 11, true }改成{ 10, true },运行结果:
Parameterized Number is : 1
Parameterized Number is : 3
Parameterized Number is : 6
Parameterized Number is : 10
Parameterized Number is : 22
Parameterized Number is : 23
testOdd[3](myTest.param.ParametTestUnit): expected:<true> but was:<false>
false
表示第3个数据(基于0)有问题。
成组测试
有很多单元测试类需要测试,将它们组成一个Suite,大家一起测试。例如新写了两个单元测试类:
- package myTest.param;
- import static org.junit.Assert.assertEquals;
- public class AddUnit1 {
- @org.junit.Test
- public void testAdd() {
- System.out.println("Inside AddUnit1");
- assertEquals(3, 1+2);
- }
- }
- package myTest.param;
- import static org.hamcrest.CoreMatchers.*;
- import static org.junit.Assert.*;
- public class AddUnit2 {
- @org.junit.Test
- public void testSth() {
- System.out.println("Inside AddUnit2");
- assertThat("Zero is one", 0, is(not(1))); // passes
- assertThat("Zero is one", 0, is(1)); //fail
- }
- }
将它们组成一个Suite,需要写捆绑代码,清晰起见,单独用一个类。
- package myTest.param;
- import org.junit.runner.RunWith;
- import org.junit.runners.Suite;
- @RunWith(Suite.class)
- @Suite.SuiteClasses({
- AddUnit1.class,
- AddUnit2.class,
- //ParametTestUnit.class
- })
- public class SuiteUnit {}
还是用上面的Main,修改为runClasses(SuiteUnit.class); 运行结果:
Inside AddUnit1.testAdd()
Inside AddUnit2.testSth()
testSth(myTest.param.AddUnit2): Zero is one
Expected: is <1>
got: <0>
false
可以把对ParametTestUnit.class的测试也加进来。运行结果:
Inside AddUnit1.testAdd()
Inside AddUnit2.testSth()
Parameterized Number is : 1
Parameterized Number is : 3
Parameterized Number is : 6
Parameterized Number is : 10
Parameterized Number is : 22
Parameterized Number is : 23
testSth(myTest.param.AddUnit2): Zero is one
Expected: is <1>
got: <0>
testOdd[3](myTest.param.ParametTestUnit): expected:<true> but was:<false>
false