JUnit 提供註解 org.junit.Ignore 用於暫時忽略某個測試方法或者說整個類。由於有時候因爲測試環境受限,並不能保證每個測試方法都能正確運行。java
1,方法級別上使用@ignore來註釋咱們的測試方法,結果就是該方法在測試執行時會被跳過。測試結束後,還能夠獲取詳細的統計信息,不只包括了測試成功和測數據庫
試失敗的次數,也包括了被忽略的測試數目。測試
例以下面的代碼便表示因爲沒有了數據庫連接,提示 JUnit 忽略測試方法 unsupportedDBCheck:
spa
package test.junit4test; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; public class LinkinTest { @Ignore @Test public void test1() { Assert.assertTrue(true); } @Test public void test2() { Assert.assertTrue(true); } }
2,類級別上使用@ignore來修飾整個類。這個類中全部的測試都將被跳過。code
package test.junit4test; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @Ignore public class LinkinTest { @Test public void test1() { Assert.assertTrue(true); } @Test public void test2() { Assert.assertTrue(true); } }
關於上面的忽略測試必定要當心。註解 org.junit.Ignore 只能用於暫時的忽略測試,若是須要永遠忽略這些測試,必定要確認被測試代碼再也不須要這些測試方法,以避免忽略必要的測試點。blog