JR 精品文章 - Ant+JUnit+Cobertura
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » 开发方法及软件工程 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
Ant+JUnit+Cobertura
cherami 原创   更新:2007-01-04 09:49:34  版本: 1.0   

Ant+JUnit+Cobertura

看标题就知道,这个是开发一个Java应用的利器组合,使用Ant完成工程的构建(Build),使用JUnit完成单元测试,使用Cobertura完成代码覆盖测试,也可以辅助查找性能瓶颈一些类型的BUG,下面是一个完整的build.xml范例,可以完全拿来用,不需任何修改,只要你的目录和这里的目录一致(应该也是很通用的):

下载下面的build.xml文件

文件内容:
<project default="all">
    <!– =================================================================== –>
    <!–                               Definitions                           –>
    <!– =================================================================== –>   
    <property name="base-dir" location="." />
    <property name="lib-dir" location="${base-dir}/lib" />
    <property name="src-dir" location="${base-dir}/src" />
    <property name="testcase-dir" location="${base-dir}/test" />
    <property name="out-dir" location="${base-dir}/classes" />
    <property name="report-dir" location="${base-dir}/report" />
    <property name="junit-dir" location="${report-dir}/junit" />
    <property name="junit-xml-dir" location="${junit-dir}/xml" />
    <property name="junit-html-dir" location="${junit-dir}/html" />
    <property name="cobertura-dir" location="${report-dir}/cobertura" />
    <property name="cobertura-xml-dir" location="${cobertura-dir}/xml" />
    <property name="cobertura-html-dir" location="${cobertura-dir}/html" />
    <property name="instrumented-dir" location="${report-dir}/instrumented" />
    <property name="instrument-file" location="${instrumented-dir}/cobertura.ser" />
    <property name="cobertura.dir" value="${instrumented-dir}" />
   
   
    <path id="classpath.all">
        <pathelement location="${out-dir}" />
        <fileset id="alljars" dir="${lib-dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <!– include the cobertura building jars –> 
    <path id="cobertura.classpath">
        <path refid="classpath.all" />
    </path>   
   
    <!– define the cobertura property file –> 
    <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
   
    <!– =================================================================== –>
    <!–                            Project Targets                          –>
    <!– =================================================================== –>
   
    <target name="init" description="initialize the project env.">
        <!– create the output folder –> 
        <mkdir dir="${out-dir}" />
        <mkdir dir="${report-dir}" />
        <copy todir="${out-dir}">
            <fileset dir="${src-dir}" includes="**/*.properties" />
        </copy>
    </target>

     <target name="compile" depends="init">
        <javac srcdir="${src-dir}" destdir="${out-dir}">
            <classpath refid="classpath.all" />
        </javac>
    </target>
  
    <!– =================================================================== –>
    <!–                            Unit Test Targets                        –>
    <!– =================================================================== –>
   
    <!– - - - - - - - - - - - - - - - - -
      target: init
      initialize the build env            
    - - - - - - - - - - - - - - - - - –>   
    <target name="init-test" description="initialize the test env.">
        <!– create the output folder –> 
        <mkdir dir="${junit-dir}" />
        <mkdir dir="${junit-xml-dir}" />
        <mkdir dir="${junit-html-dir}" />
    </target>

    <!– - - - - - - - - - - - - - - - - -
      target: compile-test
      compile the test cases                
    - - - - - - - - - - - - - - - - - –>    
    <target name="compile-test" depends="compile">
        <javac srcdir="${testcase-dir}" destdir="${out-dir}">      
            <classpath refid="classpath.all" />
        </javac>
    </target>

    <!– =================================
      target: test
      run the unit test
     ================================= –>   
    <target name="test" depends="init-test">
        <junit fork="yes" printsummary="on" maxmemory="100m">
            <sysproperty key="net.sourceforge.cobertura.datafile"
                file="${instrument-file}" />

            <classpath location="${instrumented-dir}" />
            <classpath refid="cobertura.classpath" />
                  
            <formatter type="xml" />
            <batchtest todir="${junit-xml-dir}">
                <fileset dir="${out-dir}">
                    <include name="**/Test*.class" />
                    <exclude name="**/*$*.class" />
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir="${junit-xml-dir}">
            <fileset dir="${junit-xml-dir}">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="frames" todir="${junit-html-dir}" />
        </junitreport>
    </target>
   
    <!– =================================================================== –>
    <!–                      Code Coverage Targets                          –>
    <!– =================================================================== –>
   
    <!– - - - - - - - - - - - - - - - - -
      target: init
      initialize the build env. for code coverage                     
    - - - - - - - - - - - - - - - - - –>   
    <target name="init-coverage" description="initialize the build env.">
        <!– create the output folder –> 
        <mkdir dir="${instrumented-dir}" />
        <mkdir dir="${cobertura-dir}" />
        <mkdir dir="${cobertura-xml-dir}" />
        <mkdir dir="${cobertura-html-dir}" />
    </target>
   
    <target name="compile-coverage" depends="init">
        <javac srcdir="${src-dir}:${testcase-dir}" destdir="${out-dir}" debug="true" > 
            <classpath refid="cobertura.classpath" />
        </javac>
    </target>

    <!– =================================
      target: instrument
      Instrument into the class files, but
      exclude test classes
     ================================= –>    
    <target name="instrument" depends="init-coverage,compile-coverage" description="instrument into the class files">
        <!–
          Instrument the application classes, writing the
          instrumented classes into ${instrumented.dir}.
        –>
        <cobertura-instrument todir="${instrumented-dir}" datafile="${instrument-file}">
            <!–
                The following line causes instrument to ignore any
                source line containing a reference to log4j, for the
                purposes of coverage reporting.
            –>
            <ignore regex="org.apache.log4j.*" />
            <fileset dir="${out-dir}">
                <!–
                  Instrument all the application classes, but
                  don’t instrument the test classes.
                –>
                <include name="**/*.class" />
                <exclude name="**/Test*.class" />
            </fileset>
        </cobertura-instrument>
    </target>       
   
    <!– =================================
      target: coverage-check
      check the code coverage by given rates.
     ================================= –> 
    <target name="coverage-check" description="check the code coverage by given rates">
      <cobertura-check branchrate="34" totallinerate="100" />
    </target>
   
    <!– =================================
      target: coverage-report-xml
      generate code coverage report by xml format
     ================================= –>     
    <target name="coverage-report-xml" description="generate xml report">
      <!– Generate an XML file containing the coverage data using the "srcdir" attribute. –>
      <cobertura-report srcdir="${src-dir}" destdir="${cobertura-xml-dir}" format="xml" datafile="${instrument-file}"/>
    </target>
   
    <!– =================================
      target: coverage-report-html
      generate code coverage report by html format
     ================================= –>   
    <target name="coverage-report-html">
    <!–
        Generate a series of HTML files containing the coverage
        data in a user-readable form using nested source filesets.
    –>
      <cobertura-report destdir="${cobertura-html-dir}" datafile="${instrument-file}">
        <fileset dir="${src-dir}">
          <include name="**/*.java"/>
        </fileset>
      </cobertura-report>
    </target>
   
    <!– run the code coverage individual –>
    <target name="coverage" depends="compile-coverage,instrument,test,coverage-report-html"
        description="Compile, instrument ourself, run the tests and generate JUnit and code coverage reports."/>
   
    <!– =================================================================== –>
    <!–                           Public Targets                            –>
    <!– =================================================================== –>
   
    <target name="clean">
        <delete quiet="true" includeEmptyDirs="true">
            <fileset dir="${report-dir}">
                <exclude name=".cvsignore" />
                <exclude name="CVS" />
            </fileset>
            <fileset dir="${out-dir}">
            </fileset>
        </delete>
    </target>
   
   
    <!– =================================================================== –>
    <!–                           Global Targets                            –>
    <!– =================================================================== –>
   
    <target name="all" depends="compile" />
   
    <target name="junit" depends="clean, compile-test, test" />

    <target name="full" depends="clean, coverage" />

</project>


作者: Cherami
原载: Ant+JUnit+Cobertura
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。

更多技术文章,请访问我的BLOG:解惑

版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     1       0
作者其它文章: 作者全部文章     查看作者的Blog

这个文章共有 0 条评论
主题: 从宾馆服务员到微软技术专家 上一篇文章
返回文章列表 返回〔开发方法及软件工程〕
下一篇文章 主题: 软件开发必备的因素


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号