使用ant进行junit测试

发表于:2007-12-04 13:45

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

我的例子
被测试类:
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
  测试结果:
11
  我们会看到一个err,和一个fail...通过一个...然后我们再去修改被测试类,之后的测试结果为:
111
  看到了吗?红条变为绿条,表示测试全部通过....
  NOTICE:error和fail的区别为:error是测试之外的错误,是无法预料的.而fail表示测试失败...你就需要去找被测试类中方法的错了...
32/3<123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号