• 2004-11-16

    整合单元测试的自动化

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://bpnrtech.blogbus.com/logs/499260.html

    整合单元测试的自动化

     

    注:没有对antjunit的使用进行阐述

    我们也做单元测试,一般来说都是在类刚刚写完,或责被修改成时进行的。但是很少做整合性的单元测试(整个项目的单元测试同时进行),由此带来的问题是,当修改了某个类后,该类本身测试也许通过,但是不一定调用者都能通过,但是一般我们很难知道这个类被哪些类调用了,而且时间一长将很难查找问题所,而且这个问题在接口整合的时候尤其明显,一般接口使用者在出现问题的时候首先考虑的是自己的代码出了问题,这样将大大延长解决问题的时间。

    在软件开发的单元测试部分,很多程序员都会在main里面写测试用例(我也是),来对各个方法进行测试,但随着程序越变越大,这种开发方法很快就开始显现出了缺陷。

    混乱

    类接口越多,main() 就越大。而且有时候很多方法是不能一起被执行的,而且很多测试代码在测试完后可能就已经被删除了。

    代码膨胀

    main()里面保留测试代码是个好习惯,由于加入了测试,所以产品代码比所需要的要大。但我不想在交付产品的时候包含测试代码

    测试不可靠

    既然 main() 是代码的一部分,main() 就对其他开发者通过类接口无法访问的私有成员和方法享有访问权。出于这个原因,这种测试方法就算测试通过也存在危险。

    很难自动测试

    每个类都有一个main(),很难实现集成自动化测试。

     

    所以我们必须要对测试从main函数里进行分离,利用junit可以很容易做到这一点.junit可以为每一个类实现测试用例,然后用测试套件集成这些测试用例进行单元测试,再结合ant就可以实现整合单元测试的自动化。

    下面结合一个具体例子来说明ant+junit的测试过程

     

    准备工作

    利用ant JUnit 自动化单元测试

    下载和安装 Ant

    首先下载 Ant;将 Ant bin 目录添加到路径中;然后添ANT_HOME

    下载和安装 JUnit

    下载 JUnit 3.2,并将 junit.jar 添加到 CLASSPATH或者将其拷贝到ant/lib目录下(这个很重要)。

    工程目录结构

    我们从cvs中拿下一个工程(工程目录如下),然后创建build.xml如下,测试目标:编译j2sc下的类到build/classess文件夹,然后对build/classess下所有的测试用例进行测试,生成测试报告,以上动作全部自动完成。(如何写测试用例这里不讨论)。

    运行测试

                

    具体配置和步骤

    build.xml文件

     

    <project name="ExampleTest"  default="unit"  basedir=".">

     

      <!-- set global properties for this build -->

      <property name="project" location="."/>

      <property name="src" location="j2src"/>

      <property name="build" location="build"/>

      <property name="reports" location="reports"/>

      <property name="classpath" location="${build}/classes"/>

      <property name="lib" location="lib"/>

     

      <target name="init" depends="clean">

        <mkdir dir="${build}"/>

        <mkdir dir="${build}/classes"/>

        <mkdir dir="${reports}"/>

        <mkdir dir="${reports}/html"/>

      </target>

     

      <target name="compile" depends="init" description="compile the source " >

        <!-- Compile the java code from ${src} into ${build} -->

        <javac srcdir="${src}" destdir="${build}/classes">

        <classpath>

          <fileset dir="${lib}">

            <include name="**/*.jar"/>

          </fileset>

        </classpath>

        </javac>

      </target>

     

      <target name="clean" description="clean up" >

        <!-- Delete the ${build} and ${dist} directory trees -->

        <delete dir="${build}"/>

        <delete dir="${build}/classes"/>

        <delete dir="${reports}" />

        <delete>

           <fileset dir=".">

           <include name="TEST-*.xml"/>

        </fileset>

        </delete>

      </target>

     

      <target name="unit" depends="compile">

        <junit printsummary="yes">

        <classpath>

        <fileset dir="${lib}">

                 <include name="**/*.jar"/>

        </fileset>

            <pathelement path="${classpath}"/>

        </classpath>

        <batchtest>

            <fileset dir="${src}">

             <include name="**/*Test.java"/>

            </fileset>

            <formatter type="xml"/>

        </batchtest>

        </junit>

            <junitreport todir="${reports}">

            <fileset dir=".">

               <include name="TEST-*.xml"/>

            </fileset>

            <report format="frames" todir="${reports}/html"/>

        </junitreport>

       </target>

    </project>

     

    上面定义了4个任务

     

    1.clean   清除build,report和生成的一些临时文件

    2.init     生成build,report,report/html文件夹

    3.compile 编译src下的类,并把这些类拷入到build/clssess目录下

    4.unit    测试build/classesstest包里的test case,同时生成测试结果到report/html下面,不过你可以选择多种输出格式,我这里选择了xml格式,然后生成html页面测试报告,这样项目成员可以直接在web服务器下查看所有类的测试报告,这是很爽的事情,因为你马上就可以知道在这次测试中哪些类存在问题。

     

    现在可以运行测试:

    dos下转到当前根目录,运行ant就可以进行自动测试了(默认任务是unit

    测试结果显示:

    参考:

    http://ant.apache.org/manual/using.html#path

    http://ant.apache.org/manual/OptionalTasks/junit.html

    http://ant.apache.org/manual/index.html

    http://www-900.ibm.com/developerWorks/cn/java/j-ant/index.shtml

    - 作者: icefire 2004年04月16日,星期五 10:41:10


    收藏到:Del.icio.us