如果是基於Eclipse使用Junit,在現在的Eclipse版本中都已經集成了Junit,可以根據自身的需求自由使用Junit3或者Junit4。在本文中主要通過測試簡單算術程序(加,減,乘,除)來介紹Junit4的使用,並引入一個簡單的案例進行講解。
代碼地址:https://github.com/ithinker1991/TestCourse
Step 1: 寫出基本的算術代碼Calculate.java
package com.ysc.main; public class Calculate { public static int add(int a, int b) { return a + b; } public static int minus(int a, int b) { return a - b; } public static int divide(int a, int b) throws Exception { if (b == 0) { throw new Exception("除數不能為0"); } return a / b; } public static int multiply(int a, int b) { return a * b; } }
Step 2: 對Calculate類添加Junit4的測試單元,右鍵->new->JUnit Test Case,如下圖
Step 3: 對添加的測試用例進行配置,命名為TestCalculate,點擊Next
Step 4: 選擇需要測試的函數,點擊Finish,完成JUnit的基本配置
Step 5: 在經過上面的步驟之后,就可以得到配置好的測試用例
package com.ysc.main; import static org.junit.Assert.*; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class CalculateTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Before public void setUp() throws Exception { } @Test public void testAdd() { fail("Not yet implemented"); } @Test public void testMinus() { fail("Not yet implemented"); } @Test public void testDivide() { fail("Not yet implemented"); } @Test public void testMultiply() { fail("Not yet implemented"); } }
Step 6: 現在需要做的就是添加上需要測試的數據
package com.ysc.main; import static org.junit.Assert.*; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class CalculateTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Before public void setUp() throws Exception { } @Test public void testAdd() { assertEquals(5, Calculate.add(1, 4)); } @Test public void testMinus() { assertEquals(-1, Calculate.minus(2, 3)); } @Test public void testDivide() throws Exception { assertEquals(0, Calculate.divide(1, 4)); } @Test public void testMultiply() { assertEquals(4, Calculate.multiply(1, 4)); } }
Step 7: 開始進行測試,右鍵->Run as->JUnit Test
由於添加的數據都是正常運行的數據,所有用例都測試正常
Step 6: 單純一組測試參數肯定不足以確認代碼已經達到了預期標准,應該對所有輸入進行等價類划分,進行參數化測試。在此例中只對add函數進行參數化測試。
左值 | 右值 | 預期結果 | |
case 1 | 1 | 1 | 2 |
case 2 | -1 | -1 | -2 |
case 3 | 1 | -1 | 0 |
case 4 | 1 | -2147483648 | |
case 5 | -2147483648 | -1 | 2147483647 |
修改TestCalculate,注意在類上方需要加注解 @RunWith(Parameterized.class)
package com.ysc.main; import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.Collection; import org.junit.Before; import org.junit.BeforeClass; 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 CalculateTest { @Parameters public static Collection data() { return Arrays.asList(new Object[][] { { 1, 1, 2 }, { -1, -1, -2 }, { -1, 1, 0 }, { 2147483647, 1, -2147483647 }, { -2147483648, -1, 2147483647 } }); } private int leftVal; private int rightVal; private int expectedVal; public CalculateTest(int leftVal, int rightVal, int expectedVal) { super(); this.leftVal = leftVal; this.rightVal = rightVal; this.expectedVal = expectedVal; } }
所有測試用例通過測試,如下圖