Parameterized Testing | 参数化测试

JUnit Parameterized tests      

https://github.com/junit-team/junit/wiki/Parameterized-tests

 1 package org.ut.parameter;
 2 public class Fibonacci {
 3     public static void main(String[] args) {
 4         int nums = compute(6);  
 5         System.out.println(nums);
 6     }
 7     //0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
 8     public static int compute(int n) {
 9         if(n==0) return 0;   
10         if(n<=1) return 1;   
11         return compute(n-1)+compute(n-2);
12     }
13 }
Source code
package org.ut.parameter;
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 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;
    /*
     * Each instance of FibonacciTest will be constructed using the two-argument constructor and
     * the data values in the @Parameters method.
     */
    public FibonacciTest(int input, int expected) {
        fInput= input;
        fExpected= expected;
    }

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}
Parameterized test type 1
 1 package org.ut.parameter;
 2 import static org.junit.Assert.*;
 3 import java.util.Arrays;
 4 import java.util.Collection;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.junit.runners.Parameterized;
 8 import org.junit.runners.Parameterized.Parameter;
 9 import org.junit.runners.Parameterized.Parameters;
10 
11 @RunWith(Parameterized.class)
12 public class FibonacciTest2 {
13     /*
14      * Identify Individual test cases
15      * @Parameters(name = "{index}: fib({0})={1}")
16      */
17     @Parameters(name = "{index}: fib[{0}]={1}")
18     public static Collection<Object[]> data() {
19         return Arrays.asList(new Object[][] {
20 
21                  { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 },{ 6, 8 }  
22            });
23     }
24     /*
25      * It is also possible to inject data values directly into fields without
26      * needing a constructor using the @Parameter annotation
27      */
28     @Parameter // first data value (0) is default
29     public /* NOT private */ int fInput;
30 
31     @Parameter(value = 1)
32     public /* NOT private */ int fExpected;
33 
34     @Test
35     public void test() {
36         assertEquals(fExpected, Fibonacci.compute(fInput));
37     }
38 }
Parameterized test type 2

@Parameters(name = "{index}: fib[{0}]={1}")

@Parameters(name = "{index}: fib({0})={1}")

See https://bugs.eclipse.org/bugs/show_bug.cgi?id=102512

原文地址:https://www.cnblogs.com/markjiao/p/3875363.html