Junit 使用html
一、忽略測試方法。在使用@Test的方法上使用@Ignore,將不會對此方法進行測試java
二、測試套件數組
解決的問題:函數
一、對測試類進行統一測試,而沒必要在單獨測試類上一個一個進行測試。測試
使用JUnit的@RunWith以及@SuiteClassses註解,@SuiteClassses後面爲待測試類的數組ui
示例:this
@RunWith(Suite.class) @Suite.SuiteClasses({UserTest.class}) --指定要測試的類 public class TestAll { }
三、參數化測試spa
解決問題:對同一個方法使用不一樣的參數進行測試。.net
示例:code
package com.vrvwh.wh01.testSuit; import com.vrvwh.wh01.controller.Calculator; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.Collection; /** * Created by Administrator on 2015/1/22. */ @RunWith(Parameterized.class) public class ParameterTest { private long expected; private long input1; private long input2; public ParameterTest(int expected, int x, int y){ this.expected = expected; this.input1 = x; this.input2 = y; } @Parameterized.Parameters public static Collection getData(){ Object[][] object = {{3,1,2}, {0,0,0}, {-4,-1,-3}, {6,-3,9}}; return Arrays.asList(object); } @Test public void testAdd(){ Calculator calculator=new Calculator(); long result=calculator.add(input1,input2); Assert.assertTrue(expected == result); } }
注意:getData中object 數組數據順序必須與構造函數順序匹配
參考:http://www.ibm.com/developerworks/cn/java/j-lo-junit4/index.html