Ant和Junit集成 开发
首先介绍Ant中比较重要的几个节点
    1,设置类路径
     
     
     
   

    location用来指定一个单个的文件,或者是目录,path用来指定目录,此外在location中可以使用通配符
    path用来和预定义的路径一起用,在大多数情况下,location用的比较多
    在classpath中也支持path和location路径,所以这种写法
     
     
   

    可以用下面的写法代替
   
    在classpath中可以使用 pathelement节点 fileset节点,dirset节点和filelist节点,用法如下
   
     
     
       
     

     
     
       
       
     

     
   

    2,设置路径
    path路径可以引用另外一个path路径
   
     
     
       
     

     
   

    在path中应用另外一个path路径
   
     
     
   

    在path中也有 location和 path的属性,所以也可以这么简写
   
     
   


    can be written as:    
    从 Ant1.8开始 path有一个可选的属性cache,cache设为true的话,则系统只会扫描一遍它内嵌的路径
    cache默认是为false,为了性能的考虑,又是即使我们只有了一次这个path路径我们也要把cache设置为true(在复杂的内嵌结构中);
    可以使用表达式${toString:pathreference}把path路径转换成字符串以:或者;间隔(跟据系统而定)
   





Ant 自动化构建工具
Junit 自动化测试工具
    XP极限编程,边开发,边测试:基于单元测试的编程
Junit 3 使用
    1,测试类需要继承TestCase
    2,setUp方法用于初始化
    3,tearDown方法用于结束
    4,下面介绍几个测试方法
        assertEquals("测试失败","str1","str2");如果str1和str2不相等的话,输出测试失败
        assertNotNull("对象不能为null",object);如果object为null的话,测试失败,输出 对象不能为null
        assertNull 和上面的相反
Junit 4 使用       
    1,使用注解
    2,@Before 表示测试前执行的方法
    3,@After 表示测试结束后执行的方法
    4,@Test表示测试方法
    5,命名规范 testXx()。。
    6,在这里调用Assert类中的静态方法进行测试
        在这里可以静态测试类import static org.junit.Assert.*;
        这样的话就不需要写Assert,而可以直接调用静态方法
        这样的话可以方便移植到Junit3的测试环境中,只需要继承TestCase类
       
    7,测试方法介绍
        Assert.assertEquals("测试失败",str1,str2);
        方法和Junit 3中的基本一样,只是Junit 4 使用的注解的方式,方便写测试单元
       
        区别Junit3  : 断言会抛出异常, 否则 fail。。
        public void test(){
            try{
                youMethod();
                fail("没有捕获异常");
            }catch(NumberFormatException e){}
        }
        Junit4 : 断言会出现异常
        @Test(excepted=NumberFormatException.class)
        public void test throws NumberFormatException(){
            youMethod();
        }
Junit和Ant集成
核心代码:在ant下进行junit测试,并生成html框架的测试报告
       
           
           
           
           
           
           
                   
           

       

       
       
           
           
       

下面把完整的Ant和Junit集成小例子的代码发布出来

   
   
   
   
  1. xml version="1.0" encoding="UTF-8"?> 
  2. <project name="junit-test" > 
  3.      
  4.     <property name="src.dir" location="src">property> 
  5.     <property name="build.dir" location="build">property> 
  6.     <property name="build.classes" location="build/classes">property> 
  7.     <property name="build.test.dir" location="build/test">property> 
  8.     <property name="build.test.classes" location="build/test/classes">property> 
  9.     <property name="build.test.report" location="build/test/report">property> 
  10.     <property name="test.src.dir" location="test">property> 
  11.     <property name="compile-lib" location="lib">property> 
  12.     <property name="run.test.class" value="com.soukenan.testant.main.HelloWorldTest">property> 
  13.     <property name="run.test.classes" value="**/*Test.class">property> 
  14.      
  15.      
  16.     <path id="compile-path" > 
  17.         <fileset dir="${compile-lib}" includes="*.jar">fileset> 
  18.     path> 
  19.     <path id="compile-test-path"> 
  20.         <path refid="compile-path">path> 
  21.         <pathelement location="${build.classes}"/> 
  22.     path> 
  23.     <path id="run-test-path"> 
  24.             <path refid="compile-test-path">path> 
  25.             <pathelement location="${build.test.classes}"/> 
  26.     path> 
  27.      
  28.     -- =================================  
  29.           target: default               
  30.          ================================= --> 
  31.     <target name="init" > 
  32.         <echo>清理文件夹echo> 
  33.         <echo>${ant.version}echo> 
  34.         <delete dir="build">delete> 
  35.     target> 
  36.  
  37.     <target name="mkdir" depends="init"> 
  38.         <echo>创建文件夹echo> 
  39.         <mkdir dir="${build.dir}"/> 
  40.         <mkdir dir="${build.classes}"/> 
  41.         <mkdir dir="${build.test.dir}"/> 
  42.         <mkdir dir="${build.test.classes}"/> 
  43.         <mkdir dir="${build.test.report}"/> 
  44.     target> 
  45.      
  46.     <target name="compile" depends="mkdir"> 
  47.         <echo>编译源代码echo> 
  48.         <javac destdir="${build.classes}" srcdir="${src.dir}" includeantruntime="true">javac> 
  49.     target> 
  50.      
  51.     <target name="compile-test" depends="compile"> 
  52.         <echo>编译测试源代码echo> 
  53.         <javac srcdir="${test.src.dir}" 
  54.          destdir="${build.test.classes}" classpathref="compile-test-path" includeantruntime="true" > 
  55.         javac>   
  56.     target> 
  57.      
  58.     <target name="run-test" depends="compile-test"> 
  59.         <echo>运行单元测试echo> 
  60.         <junit printsummary="true" > 
  61.             <classpath refid="run-test-path">classpath> 
  62.             <formatter type="brief" usefile="false"/> 
  63.             <formatter type="xml"/>  
  64.              
  65.              
  66.             <batchtest todir="${build.test.report}"> 
  67.                 <fileset dir="${build.test.classes}"  includes="${run.test.classes}">fileset>  
  68.             batchtest> 
  69.         junit> 
  70.          
  71.         <junitreport todir="${build.test.report}">  
  72.             <fileset dir="${build.test.report}" includes="*.xml">fileset> 
  73.             <report format="frames" todir="${build.test.report}/html"/> 
  74.         junitreport> 
  75.     target> 
  76.     <target name="end" depends="run-test"> 
  77.         <echo>构建完成echo> 
  78.     target> 
  79. project>