junit 单元测试 - 参数化测试

junit4.x版本需要引入如下jar包:

hamcrest-core-1.3.jar

junit-4.12-beta-3.jar

新建一个计算器类,如下:

package com.pt;

public class Calculate {
    /**
     * 加法
     * @param d1
     * @param d2
     * @return
     */
    public static double add(double d1,double d2){
        return d1 + d2;
    }
    
    /**
     * 减法
     * @param d1
     * @param d2
     * @return
     */
    public static double diff(double d1,double d2){
        return d1 - d2;
    }
    
    /**
     * 乘法
     * @param d1
     * @param d2
     * @return
     */
    public static double muti(double d1,double d2){
        return d1 * d2;
    }
    
    /**
     * 除法
     * @param d1
     * @param d2
     * @return
     */
    public static double devide(double d1,double d2){
        return d1 / d2;
    }
}
Calculate.java

新建一个Source Folder文件夹(注意:不是folder文件夹)

新建一个JUnit Test Case 类(注意:不是他普通的类)

 1 package com.pt;
 2 
 3 
 4 import java.util.Arrays;
 5 import java.util.Collection;
 6 
 7 import org.junit.After;
 8 import org.junit.Before;
 9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.junit.runners.Parameterized;
12 import org.junit.runners.Parameterized.Parameters;
13 
14 @RunWith(Parameterized.class)    //指定Runner 参数化测试的时候需要此注解
15 public class TestCalculate {
16     private long input1;
17     private long input2;
18     
19     
20     /**
21      * 构造函数
22      * @param in_1
23      * @param in_2
24      */
25     public TestCalculate(long in_1, long in_2){
26         this.input1 = in_1;
27         this.input2 = in_2;
28     }
29     
30     @Before            //每个测试函数执行前 都执行 setUp不可以是static
31     //@BeforeClass    //测试前只执行一次 setUp必须是static
32     public void setUp(){
33         System.out.println("开始测试之前~~~~~~~~~");
34     }
35     
36     @After            //每个测试函数执行后 都执行 tearDown不可以是static
37     //@AfterClass        //测试结束后只执行一次  tearDown必须是static
38     public void tearDown(){
39         System.out.println("开始完成之后~~~~~~~~~");
40     }
41     
42     //参数化测试 测试数据
43     @Parameters
44     public static Collection preData(){
45         //必须定义成 定义成long就会报错,在执行构造函数的时候
46         Object[][] data = new Object[][]{{1,2},{2,3},{4,5}};
47         return Arrays.asList(data);        //将数组转化成集合
48     }
49     
50     
51     @Test
52     public void testAdd(){
53         double result_add = Calculate.add(input1, input2);
54         System.out.println("addesult: " + result_add);
55         //断言  如果与期望值3 不一样,就会包异常
56         //Assert.assertEquals("加法计算错误", (long)3, (long)result_add);
57         System.out.println("测试add方法:" + input1 + " + " + input2 + " = " + result_add);
58     }
59     
60     @Test
61     public void testDiff(){
62         Calculate.diff(1, 2);
63         System.out.println("测试diff方法");
64     }
65     
66     @Test
67     public void testMuti(){
68         Calculate.diff(1, 2);
69         System.out.println("测试muti方法");
70     }
71     
72     //如果运行时间超过300ms就会报异常
73     @Test(timeout=300)
74     public void testDevide(){
75         try{
76             Thread.sleep(298);
77         }catch(Exception ex){
78             
79         }
80         Calculate.diff(1, 2);
81         System.out.println("测试devide方法");
82     }
83 }
TestCalculate.java

OK;

右键 -> Run As ->JUnit Test

posted @ 2016-06-21 16:02  沙中世界  阅读(461)  评论(0编辑  收藏  举报