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

build-testNG.xml对应的ant脚本为

<?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}/test-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>
    <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="complie-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">
            <testng  classpathref="run-test-path"  outputDir="${test.report.dir}" 
                haltonfailure="true"
                useDefaultListeners="false"   
                listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.FailedReporter" >  
                <xmlfileset dir="${src.dir}" includes="testNG.xml"/>
                <jvmarg value="-Dfile.encoding=UTF-8" />
                <sysproperty key="org.uncommons.reportng.title" value="AutoMation TestReport" /> 
            </testng>
            </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">
                <testng  classpathref="run-test-path"  outputDir="${test.report.dir}" 
                    haltonfailure="true"
                    useDefaultListeners="false"   
                    listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.FailedReporter" >  
                    <xmlfileset dir="${src.dir}" includes="testNG.xml"/>
                    <jvmarg value="-Dfile.encoding=UTF-8" />
                    <sysproperty key="org.uncommons.reportng.title" value="AutoMation TestReport" /> 
                </testng>
                </target>


      <!-- 输出报告 -->
            <target  name="coverage-report" depends="cover-test">
                <cobertura-report srcdir="${src.dir}" destdir="${cobertura_report}"/>
            </target>
    
</project>

 

转载于:https://www.cnblogs.com/kebibuluan/p/9223018.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值