2. 使用例子:
import junit.frmework.TestCase;
public class TestSample extends TestCaset{
public void testMethod1(){
assertTrue( true);
}
}
3. setUp與tearDown,這兩個函數是junit framework中提供初始化和反初始化每個測試方法的。setUp在每個測試方法調用前被調用,負責初始化測試方法所需要的測試環(huán)境;tearDown在每個測試方法被調用之后被調用,負責撤銷測試環(huán)境。它們與測試方法的關系可以描述如下:
測試開始 -> setUp -> testXXXX -> tearDown ->測試結束
4. 使用例子:
import junit.frmework.TestCase;
public class TestSample extends TestCaset{
protected void setUp(){
//初始化……
}
public void testMethod1(){
assertTrue( true);
}
potected void tearDown(){
//撤銷初始化……
}
}
5. 區(qū)分fail、exception。
- fail,期望出現的錯誤。產生原因:assert函數出錯(如assertFalse(true));fail函數產生(如fail(……))。
- exception,不期望出現的錯誤,屬于unit test程序運行時拋出的異常。它和普通代碼運行過程中拋出的runtime異常屬于一種類型。
對于assert、fail等函數請參見junit的javadoc。
6. 使用例子:
import junit.frmework.TestCase;
public class TestSample extends TestCaset{
protected void setUp(){
//初始化……
}
public void testMethod1(){
……
try{
boolean b= ……
assertTrue( b);
throw new Exception( “This is a test.”);
fail( “Unable point.”); //不可能到達
}catch(Exception e){
fail( “Yes, I catch u”); //應該到達點
}
……
}
potected void tearDown(){
//撤銷初始化……
}
}