您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
軟件測試之使用ant進(jìn)行junit測試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/3/20 14:24:56 ] 推薦標(biāo)簽:

  5、運(yùn)行測試

我的例子
被測試類:
package cn.itcast;
public class FindChar{
  int search(char[] chars,char ch) {
   //throw new UnsupportedOperationException("");
   int i=0;
   int index=0;
   boolean b=false;
 
   if(chars==null){
     throw new IllegalArgumentException();
   }
   for(;i<chars.length;i++){
    if(chars[i]==ch){
   //   index=i;
      b=true;
      break;
    }
    else{
       index=-1;
        b=false;
    }
   }
   if(b=true){
     return index;
   }
   else
       return index;
  }
}
測試用例:
package cn.itcast;
import junit.framework.*;
public class TestFindChar extends TestCase{
  private FindChar fc=null;
  private char[] chars={'a','b','c'};
  public void setUp(){
    fc=new FindChar();
  }
  public void testSearchFound(){
    int index=fc.search(chars,'c');
    assertEquals(index,2);
  }
  public void testSearchNotFound(){
     int index=fc.search(chars,'e');
     assertTrue(index==-1);
  }
  public void testSearchIllegalArguments(){
     char[] chs=null;
     fc.search(chs,'a');
     fail("this is failure");
  }
}
  打開命令行窗口,set好classpath之后,把兩個類都編譯之后,開始測試,
  輸入:java  junit.swingui.TestRunner  TestFindChar
  測試結(jié)果:

  我們會看到一個err,和一個fail...通過一個...然后我們再去修改被測試類,之后的測試結(jié)果為:

  看到了嗎?紅條變?yōu)榫G條,表示測試全部通過....
  NOTICE:error和fail的區(qū)別為:error是測試之外的錯誤,是無法預(yù)料的.而fail表示測試失敗...你需要去找被測試類中方法的錯了...

上一頁123下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd