1、视频参考孔浩老师ant视频笔记

junit配合catubuter统计单元测试的代码覆盖率

junit配合catubuter统计单元测试的代码覆盖率

 对应的build-junit.xml脚步如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project default="coverage-report">
   <property name="src.dir" location="src"></property>
   <property name="test.src.dir" location="test"></property>
   <property name="web.dir" location="WebRoot"></property>
   <property name="conf.dir" location="conf"></property>
   <property name="web.web-info.dir" location="${web.dir}/WEB-INF"></property>
   <property name="lib.dir" location="${web.web-info.dir}/lib"></property>
   <property name="build.dir" location="build"></property>
   <property name="build.classes" location="${build.dir}/classes"></property>
   <property name="test.dir" location="${build.dir}/test/"></property>
   <property name="test.classes.dir" location="${test.dir}/classes"></property>
   <property name="test.report.dir" location="${test.dir}/report"></property>
   <property name="build.jar.dir" location="${build.dir}/dist"></property>
   <property name="build.zip.dir" location="${build.dir}/zip"></property>
   <property name="build.doc.dir" location="${build.dir}/doc"></property>
   <property name="build.src" location="${build.dir}/src"></property>
    <property name="instrumented_classes" value="${test.dir}/instrumented-classes" />
    <property name="cobertura_report" value="${test.dir}/cobertura-report" />
    
    
     <path id="complie-path">
        <fileset dir="${lib.dir}" includes="**/*.jar"></fileset>
     </path>
    
      <path id="complie-test-path">
          <path refid="complie-path"></path>
          <pathelement location="${build.classes}"/>
      </path>
    
     <path id="run-test-path">
              <path refid="complie-test-path"></path>
              <pathelement location="${test.classes.dir}"/>
    </path>
    
     <path id="cobertura-run-path">
              <path refid="complie-path"></path>
              <pathelement location="${test.classes.dir}"/>
              <pathelement location="${instrumented_classes}"/>
    </path>
    
    <target name="clean">
          <delete dir="${build.dir}"></delete>
    </target>
        
        <target name="init" depends="clean" >
          <mkdir dir="${build.dir}"/>
          <mkdir dir="${build.classes}"/>
          <mkdir dir="${test.dir}"/>
          <mkdir dir="${test.classes.dir}"/>
          <mkdir dir="${test.report.dir}"/>
          <mkdir dir="${build.jar.dir}"/>
          <mkdir dir="${build.zip.dir}"/>
          <mkdir dir="${build.doc.dir}"/>
          <mkdir dir="${build.src}"/>
          <mkdir dir="${instrumented_classes}"/>
          <mkdir dir="${cobertura_report}"/>
        </target>
    
    <target name="compile" depends="init">
       <javac destdir="${build.classes}" srcdir="${src.dir}" includeantruntime="true"
           classpathref="complie-path">
       </javac>
        
        <copy todir="${build.classes}" >
          <fileset dir="${src.dir}" excludes="**/*.java"></fileset>
        </copy>
    </target>
    
    <target name="compile-test" depends="compile">
       <javac destdir="${test.classes.dir}" srcdir="${test.src.dir}" includeantruntime="true"
           classpathref="complie-test-path">
       </javac>
        <copy todir="${test.classes.dir}" >
            <fileset dir="${test.src.dir}" excludes="**/*.java"></fileset>
        </copy>
        
    </target>
    

    
    <target name="run-test" depends="compile-test"> 
         
        <junit printsummary="true" haltonfailure="no" showoutput="true" failureproperty="junit.fail"> 
            <classpath refid="run-test-path"> 
            </classpath> 
            <formatter type="xml" usefile="true"/> 
            <batchtest fork="yes" todir="${test.report.dir}"> 
                <fileset dir="${test.classes.dir}"> 
                    <include name="**/Test*.class"/> 
                </fileset> 
            </batchtest> 
        </junit> 
                 
        <junitreport todir="${test.report.dir}">  
            <fileset dir="${test.report.dir}">  
                <include name="TEST-*.xml"/>  
            </fileset>  
            <report format="frames" todir="${test.report.dir}/html"/>  
        </junitreport>  
         <fail if="${junit.fail}" message="单元测试运行失败,请查看:${test.report.dir}/html"/>
    </target> 
    
    
    
    
     <!-- 定义 cobertura 的ant task   -->
        <taskdef classpath="cobertura.jar" resource="tasks.properties" classpathref="complie-path" />
        <!-- 为源码添加日志代码,放到 instrumented-classes 目录 -->
        <target name="instrument" depends="run-test">
            <cobertura-instrument todir="${instrumented_classes}">
                <fileset dir="${build.classes}">
                    <include name="**/*.class" />
                </fileset>
            </cobertura-instrument>
        </target>

    

    <!-- 执行单元测试 -->
        <target name="cover-test" depends="instrument">
            <junit printsummary="yes" fork="yes" haltonfailure="no" >
                <classpath refid="cobertura-run-path"></classpath>
                <formatter type="xml" usefile="true"/> 
                            <batchtest fork="yes" todir="${test.report.dir}"> 
                                <fileset dir="${test.classes.dir}"> 
                                    <include name="**/Test*.class"/> 
                                </fileset> 
                   </batchtest> 
            </junit>
        </target>
    
        <!-- 输出报告 -->
        <target  name="coverage-report" depends="cover-test">
            <cobertura-report srcdir="${src.dir}" destdir="${cobertura_report}"/>
        </target>
    

    
    
</project>

 

分类:

技术点:

相关文章: