熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Java應用利器組合:Ant+JUnit+Cobertura

2022-06-13   來源: Java開源技術 

  看標題就知道這個是開發一個Java應用的利器組合使用Ant完成工程的構建(Build)使用JUnit完成單元測試使用Cobertura完成代碼覆蓋測試也可以輔助查找性能瓶頸和一些類型的BUG下面是一個完整的buildxml范例可以完全拿來用不需任何修改只要你的目錄和這裡的目錄一致(應該也是很通用的)

  下載下面的buildxml文件

  文件內容
<project default=all>
    <!– =================================================================== –>
    <!–                               Definitions                           –>
    <!– =================================================================== –>   
    <property name=basedir location= />
    <property name=libdir location=${basedir}/lib />
    <property name=srcdir location=${basedir}/src />
    <property name=testcasedir location=${basedir}/test />
    <property name=outdir location=${basedir}/classes />
    <property name=reportdir location=${basedir}/report />
    <property name=junitdir location=${reportdir}/junit />
    <property name=junitxmldir location=${junitdir}/xml />
    <property name=junithtmldir location=${junitdir}/html />
    <property name=coberturadir location=${reportdir}/cobertura />
    <property name=coberturaxmldir location=${coberturadir}/xml />
    <property name=coberturahtmldir location=${coberturadir}/html />
    <property name=instrumenteddir location=${reportdir}/instrumented />
    <property name=instrumentfile location=${instrumenteddir}/coberturaser />
    <property name=coberturadir value=${instrumenteddir} />
   
   
    <path id=classpathall>
        <pathelement location=${outdir} />
        <fileset id=alljars dir=${libdir}>
            <include name=**/*jar />
        </fileset>
    </path>

  <!– include the cobertura building jars –> 
    <path id=coberturaclasspath>
        <path refid=classpathall />
    </path>   
   
    <!– define the cobertura property file –> 
    <taskdef classpat resource=tasksproperties/>
   
    <!– =================================================================== –>
    <!–                            Project Targets                          –>
    <!– =================================================================== –>
   
    <target name=init description=initialize the project env>
        <!– create the output folder –> 
        <mkdir dir=${outdir} />
        <mkdir dir=${reportdir} />
        <copy todir=${outdir}>
            <fileset dir=${srcdir} includes=**/*properties />
        </copy>
    </target>

  <target name=compile depends=init>
        <javac srcdir=${srcdir} destdir=${outdir}>
            <classpath refid=classpathall />
        </javac>
    </target>
  
    <!– =================================================================== –>
    <!–                            Unit Test Targets                        –>
    <!– =================================================================== –>
   
    <!–
      target: init
      initialize the build env            
    –>   
    <target name=inittest description=initialize the test env>
        <!– create the output folder –> 
        <mkdir dir=${junitdir} />
        <mkdir dir=${junitxmldir} />
        <mkdir dir=${junithtmldir} />
    </target>

  <!–
      target: compiletest
      compile the test cases                
    –>    
    <target name=compiletest depends=compile>
        <javac srcdir=${testcasedir} destdir=${outdir}>      
            <classpath refid=classpathall />
        </javac>
    </target>

  <!– =================================
      target: test
      run the unit test
     ================================= –>   
    <target name=test depends=inittest>
        <junit fork=yes printsummary=on maxmemory=m>
            <sysproperty key=berturadatafile
                file=${instrumentfile} />

  <classpath location=${instrumenteddir} />
            <classpath refid=coberturaclasspath />
                  
            <formatter type=xml />
            <batchtest todir=${junitxmldir}>
                <fileset dir=${outdir}>
                    <include name=**/Test*class />
                    <exclude name=**/*$*class />
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir=${junitxmldir}>
            <fileset dir=${junitxmldir}>
                <include name=TEST*xml />
            </fileset>
            <report format=frames todir=${junithtmldir} />
        </junitreport>
    </target>
   
    <!– =================================================================== –>
    <!–                      Code Coverage Targets                          –>
    <!– =================================================================== –>
   

  <!–
      target: init
      initialize the build env for code coverage                     
    –>   
    <target name=initcoverage description=initialize the build env>
        <!– create the output folder –> 
        <mkdir dir=${instrumenteddir} />
        <mkdir dir=${coberturadir} />
        <mkdir dir=${coberturaxmldir} />
        <mkdir dir=${coberturahtmldir} />
    </target>
   
    <target name=compilecoverage depends=init>
        <javac srcdir=${srcdir}:${testcasedir} destdir=${outdir} debug=true
            <classpath refid=coberturaclasspath />
        </javac>
    </target>

  <!– =================================
      target: instrument
      Instrument into the class files but
      exclude test classes
     ================================= –>    
    <target name=instrument depends=initcoveragecompilecoverage description=instrument into the class files>
        <!–
          Instrument the application classes writing the
          instrumented classes into ${instrumenteddir}
        –>
        <coberturainstrument todir=${instrumenteddir} datafile=${instrumentfile}>
            <!–
                The following line causes instrument to ignore any
                source line containing a reference to logj for the
                purposes of coverage reporting
            –>
            <ignore regex=orgapachelogj* />
            <fileset dir=${outdir}>
                <!–
                  Instrument all the application classes but
                  dont instrument the test classes
                –>
                <include name=**/*class />
                <exclude name=**/Test*class />
            </fileset>
        </coberturainstrument>
    </target>       
   
    <!– =================================
      target: coveragecheck
      check the code coverage by given rates
     ================================= –> 
    <target name=coveragecheck description=check the code coverage by given rates>
      <coberturacheck branchrate= totallinerate= />
    </target>
   
    <!– =================================
      target: coveragereportxml
      generate code coverage report by xml format
     ================================= –>     
    <target name=coveragereportxml description=generate xml report>
      <!– Generate an XML file containing the coverage data using the srcdir attribute –>
      <coberturareport srcdir=${srcdir} destdir=${coberturaxmldir} format=xml datafile=${instrumentfile}/>
    </target>
   
    <!– =================================
      target: coveragereporthtml
      generate code coverage report by html format
     ================================= –>   
    <target name=coveragereporthtml>
    <!–
        Generate a series of HTML files containing the coverage
        data in a userreadable form using nested source filesets
    –>
      <coberturareport destdir=${coberturahtmldir} datafile=${instrumentfile}>
        <fileset dir=${srcdir}>
          <include name=**/*java/>
        </fileset>
      </coberturareport>
    </target>
   
    <!– run the code coverage individual –>
    <target name=coverage depends=compilecoverageinstrumenttestcoveragereporthtml
        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=${reportdir}>
                <exclude name=cvsignore />
                <exclude name=CVS />
            </fileset>
            <fileset dir=${outdir}>
            </fileset>
        </delete>
    </target>
   
   
    <!– =================================================================== –>
    <!–                           Global Targets                            –>
    <!– =================================================================== –>
   
    <target name=all depends=compile />
   
    <target name=junit depends=clean compiletest test />

  <target name=full depends=clean coverage />

  </project>


作者: Cherami
原載: Ant+JUnit+Cobertura
版權所有轉載時必須以鏈接形式注明作者和原始出處及本聲明


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28089.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.