利用ant和junit实现单元测试的自动化

我们创建一个测试项目,名字为:test,并创建两个源文件夹:src和test..把项目源文件和测试文件分开放在这两个文件夹中。
我们在src编写一个测试类:
[java] view plain copy
  1. <pre name="code" class="java">package com.widetrust;  
  2.   
  3. public class CountService {  
  4.      private int summary;  
  5.      private int count;  
  6.      public boolean stat(int visitor){  
  7.     count++;  
  8.     summary += visitor;  
  9.     if(summary>1000 && count>2){  
  10.     return true;  
  11.     }else{  
  12.         return false;  
  13.     }  
  14.      }  
  15. }  



在test文件夹写个测试该类的类:
[java] view plain copy
  1. <pre name="code" class="java">package com.widetrust.test;  
  2.    
  3. import com.widetrust.CountService;  
  4. import junit.framework.TestCase;  
  5.    
  6. public class TestCountService extends TestCase {  
  7.     CountService cs;  
  8.     protected void setUp() throws Exception {  
  9.         cs = new CountService();  
  10.     }  
  11.    
  12.     protected void tearDown() throws Exception {  
  13.     }  
  14.     public void testStat(){        
  15.     assertEquals(true, cs.stat(4000));  
  16.     }  
  17.     public void testStat2(){  
  18.     cs.stat(2000);  
  19.     cs.stat(2000);  
  20.     assertEquals(true, cs.stat(3000));  
  21.     }  
  22. }  

当然我们可以利用eclipse的Run As Junit Test(快捷键:Alt+Shift+X T)运行我们刚写好的测试案例,但有个问题是eclipse不能正确的同步我们的开发和测试程序,而且,我们可能还希望测试的过程中为我们提供一份详细的报告文档,以供我们在测试后的改进过程中的讨论。现在我们使用ant的自动编译功能来实现测试的自动化,并让它生成一份详细的测试报告。
  注意的是,ant自带了junit的支持包,但项目需要junit.jar,还需要在项目的lib文件夹中放置junit.jar,(这里我假设工程目录存放项目依赖组件的是lib文件夹)如果我们要单独使用ant编译整个项目,需要在项目构建文件build.xml中定义编译所用到的lib包,在项目的根目录创建一个build.xml,内容如下:
[html] view plain copy
  1. <pre name="code" class="html"><?xml version="1.0"?>  
  2.    
  3. <project name="ant and junit" default="test auot junit and report" basedir=".">  
  4.      
  5.     <!-- 定义工程依赖的jar包存放的位置 -->  
  6.     <property name="lib.dir" value="lib"/>  
  7.     <path id="classpath">  
  8.        <fileset dir="${lib.dir}" includes="**/*.jar"/>  
  9.     </path>  
  10.    
  11.     <property name="output folder" value="classes"/>  
  12.     <property name="src folder" value="src"/>  
  13.     <property name="test folder" value="test"/>  
  14.     <property name="report folder" value="report"/>  
  15.    
  16.     <target name="clean">  
  17.        <delete dir="report"/>  
  18.        <echo>清除测试报告文件 成功!</echo>  
  19.     </target>  
  20.    
  21.     <target name="compile init">  
  22.        <mkdir dir="${output folder}"/>  
  23.        <echo>创建编译文件夹 成功!</echo>  
  24.     </target>  
  25.    
  26.     <target name="report init" depends="clean">  
  27.        <mkdir dir="${report folder}"/>  
  28.        <echo>创建测试报告文件夹 成功!</echo>  
  29.     </target>  
  30.    
  31.     <target name="compile" depends="compile init">  
  32.        <javac srcdir="${src folder}" destdir="${output folder}" classpathref="classpath"/>  
  33.        <echo>项目源文件编译 成功!</echo>  
  34.     </target>  
  35.    
  36.     <target name="test compile" depends="report init">  
  37.        <javac srcdir="${test folder}" destdir="${output folder}" classpathref="classpath"/>  
  38.        <echo>项目测试文件编译 成功!</echo>  
  39.     </target>  
  40.    
  41.     <target name="all compile" depends="compile, test compile">  
  42.     </target>  
  43.    
  44.     <target name="test auot junit and report" depends="all compile">  
  45.        <junit printsummary="on" fork="true" showoutput="true">  
  46.            <classpath>  
  47.               <fileset dir="${lib.dir}" includes="**/*.jar"/>  
  48.               <pathelement path="${output folder}"/>  
  49.            </classpath>  
  50.            <formatter type="xml"/>  
  51.            <batchtest todir="${report folder}">  
  52.               <fileset dir="${output folder}">  
  53.                   <include name="**/Test*.*"/>  
  54.               </fileset>  
  55.            </batchtest>  
  56.        </junit>  
  57.        <junitreport todir="${report folder}">  
  58.            <fileset dir="${report folder}">  
  59.               <include name="TEST-*.xml"/>  
  60.            </fileset>  
  61.            <report format="frames" todir="${report folder}"/>  
  62.        </junitreport>  
  63.     </target>  
  64.       
  65. </project>  



我们在eclipse中利用windows -> show View -> Ant
打开ant工作窗口,点击”Add Buildfiles” 将项目的根目录下的build.xml添加进去,然后在ant工作窗口点运行图标,可以看到控制台上的输出:(当然,用命令行方式运行也可以)利用ant和junit实现单元测试的自动化
整个项目编译成功,并在项目根目录生成一个report文件夹,我们可以从中看到一份详细的测试报告:
利用ant和junit实现单元测试的自动化
我们可能会希望junit 和ant为我们提供中文的测试报告,很遗憾的是,ant并没有给我们提供这个选项,还好,ant可以让我们通过定义styledir属性,更改报告文件的输出样式的配置文件:我们要做的,就是汉化在ant安装文件夹中的junit-frames.xsl和junit-noframes.xsl,(也可以从http://download.****.net/user/lemonfamily 下载到该文件)甚至可以在里面定义一些我们需要的东西。
[html] view plain copy
  1. <span style="background-color: rgb(255, 255, 255);">现在我们已经可以做到测试代码和开发代码分开,并利用ant的自动编译功能为我们执行测试计划和生成测试报告。虽然在编写测试案例的时候确实比较麻烦,我们需要详细的模拟实际应用中的环境,并寻找可能出现错误的边界值帮助我们在编写实际代码时尽量减少隐患的发生,往往编写测试案例的时间比我们编写实际代码的时间还长,但这样做是非常值得的,一个软件的开发周期最长的60%都是花在寻找错误和完善的过程中。</span><span style="font-family:Lucida Grande, Verdana, Arial, sans-serif;color:#333333;"><span style="font-size: 13.3333330154419px; line-height: 20px;">  
  2. </span></span>