junit忽略測試方法


JUnit 提供注解 org.junit.Ignore 用於暫時忽略某個測試方法或者說整個類。因為有時候由於測試環境受限,並不能保證每一個測試方法都能正確運行。

1,方法級別上使用@ignore來注釋我們的測試方法,結果就是該方法在測試執行時會被跳過。測試結束后,還可以獲取詳細的統計信息,不僅包括了測試成功和測

試失敗的次數,也包括了被忽略的測試數目。

例如下面的代碼便表示由於沒有了數據庫鏈接,提示 JUnit 忽略測試方法 unsupportedDBCheck:

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來修飾整個類。這個類中所有的測試都將被跳過。

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 只能用於暫時的忽略測試,如果需要永遠忽略這些測試,一定要確認被測試代碼不再需要這些測試方法,以免忽略必要的測試點。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM