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>
复制代码

 

posted on   luzhouxiaoshuai  阅读(324)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2017-06-25 c++教程网经典的c语音学习视频教程

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示