JUnit 經常使用的runner 之一 Parameterized 能實現參數化測試。運行參數化測試類時,將爲測試方法和測試數據元素的交叉產品建立實例。java
舉個例子,測試斐波那契 函數,咱們能夠這樣寫:git
import static org.junit.Assert.assertEquals; 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 FibonacciTest { @Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } }); } private int fInput; private int fExpected; public FibonacciTest(int input, int expected) { fInput= input; fExpected= expected; } @Test public void test() { assertEquals(fExpected, Fibonacci.compute(fInput)); } }
下面是須要被測試的斐波那契函數:github
public class Fibonacci { public static int compute(int n) { int result = 0; if (n <= 1) { result = n; } else { result = compute(n - 1) + compute(n - 2); } return result; } }
這裏每個斐波那契實例 都會經過含有 @Parameters 註解方法來建立,構造函數的入參值。數組
import static org.junit.Assert.assertEquals; 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.Parameter; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class FibonacciTest { @Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } }); } @Parameter // first data value (0) is default public /* NOT private */ int fInput; @Parameter(1) public /* NOT private */ int fExpected; @Test public void test() { assertEquals(fExpected, Fibonacci.compute(fInput)); } } public class Fibonacci { ... }
注意: 從JUnit4.12-beta-3 開始被引入.函數
若是你只是須要單個參數,你就不須要使用任何數組來包裹參數對象。單元測試
@Parameters public static Iterable<? extends Object> data() { return Arrays.asList("first test", "second test"); }
或者測試
@Parameters public static Object[] data() { return new Object[] { "first test", "second test" }; }
import static org.junit.Assert.assertEquals; import java.util.Arrays; 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 FibonacciTest { @Parameters(name = "{index}: fib({0})={1}") public static Iterable<Object[]> data() { return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } }); } private int input; private int expected; public FibonacciTest(int input, int expected) { this.input = input; this.expected = expected; } @Test public void test() { assertEquals(expected, Fibonacci.compute(input)); } } public class Fibonacci { ... }
若是你使用 name 註解參數,ui
As an alternative to parameterized tests you can also use the plugin JUnitParams If you want to define the parameters for your tests at the tests' Suite, you can use the ParameterizedSuite runner that is available in a separate library.this