使用Ant+JUnit+Cobertura来实现代码覆盖自动测试

前几天回学校学习到的测试代码覆盖的的方法,晒一下哈!

别丢鸡蛋哦!

<?xml version='1.0' encoding="UTF-8" ?>
< project name="Server" default="coverage" basedir=".">
  <!-- where the project source code is found -->
  <property name="src.dir" value="./src/" />
  <!-- where compiled class files should be left -->
  <property name="classes.dir" value="./bin/" />

  <!-- The path to cobertura.jar -->
  <property name="cobertura.dir" value="./cobertura-1.9/" />
  <!-- All reports go into this directory -->
  <property name="reports.dir" value="reports" />
  <!-- Instrumented classes are deposited into this directory -->
  <property name="instrumented.dir" value="instrumented" />
  <!-- Unit test reports from JUnit are deposited into this directory -->
  <property name="reports.xml.dir" value="${reports.dir}/junit-xml" />
  <property name="reports.html.dir" value="${reports.dir}/junit-html" />
  <!-- Coverage reports are deposited into these directories -->
  <property name="coverage.xml.dir" value="${reports.dir}/cobertura-xml" />
  <property name="coverage.html.dir" value="${reports.dir}/cobertura-html" />

  <path id="classpath">
    <fileset dir="./lib/" includes="**/*.jar" />
  </path>
  <path id="cobertura.classpath">
    <fileset dir="${cobertura.dir}">
      <include name="cobertura.jar" />
      <include name="lib/**/*.jar" />
    </fileset>
  </path>
  <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
  <target name="init" depends="clean">
    <mkdir dir="${classes.dir}" />
    <mkdir dir="${instrumented.dir}" />
    <mkdir dir="${reports.xml.dir}" />
    <mkdir dir="${reports.html.dir}" />
    <mkdir dir="${coverage.xml.dir}" />
    <mkdir dir="${coverage.html.dir}" />
  </target>
  <target name="compile" depends="init" description="compile classes">
    <javac destdir="${classes.dir}" srcdir="${src.dir}" debug="yes"><!--在这里 debug="yes"-->
      <classpath>
        <path refid="classpath" />
      </classpath>
    </javac>
  </target>
  <target name="jar" depends="compile" description="build release jar">
    <jar destfile="${ant.project.name}.jar">
      <fileset dir="${classes.dir}">
        <include name="**/*.class" />
      </fileset>
    </jar>
  </target>
  <target name="instrument" depends="compile">
    <!--Instrument the application classes, writing the instrumented classes into ${build.instrumented.dir}.-->
    <cobertura-instrument todir="${instrumented.dir}">
      <fileset dir="${classes.dir}">
        <!-- Instrument all the application classes, but don't instrument the test classes.-->
        <include name="**/*.class" />
        <exclude name="**/test/**/*.class" /><!--不计算所有test文件夹下的类-->
      </fileset>
    </cobertura-instrument>
  </target>
  <target name="cover-test" depends="compile">
    <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
      <sysproperty key="net.sourceforge.cobertura.datafile"   file="${basedir}/cobertura.ser" />
      <!--Note the classpath order: instrumented classes are before the original (uninstrumented) classes. This is important.-->
      <classpath refid="classpath" />
      <classpath location="${instrumented.dir}" />
      <classpath location="${classes.dir}" />
      <!-- The instrumented classes reference classes used by the  Cobertura runtime, so Cobertura and its dependencies must be on your classpath.-->
      <classpath refid="cobertura.classpath" />
      <formatter type="xml" />
      <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
      <batchtest todir="${reports.xml.dir}" unless="testcase">
        <fileset dir="${src.dir}">
          <include name="**/*Test.java" />
        </fileset>
      </batchtest>
    </junit>

    <junitreport todir="${reports.xml.dir}">
      <fileset dir="${reports.xml.dir}">
        <include name="TEST-*.xml" />
      </fileset>
      <report format="frames" todir="${reports.html.dir}" />
    </junitreport>
  </target>

  <target name="coverage-check">
    <cobertura-check branchrate="34" totallinerate="100" />
  </target>

  <target name="coverage-report">
    <!--
        Generate an XML file containing the coverage data using
        the "srcdir" attribute.
      -->
    <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
  </target>

  <target name="alternate-coverage-report">
    <!--
        Generate a series of HTML files containing the coverage
        data in a user-readable form using nested source filesets.
      -->
    <cobertura-report destdir="${coverage.html.dir}">
      <fileset dir="${src.dir}">
        <include name="**/*.java"/>
      </fileset>
    </cobertura-report>
  </target>
  <target name="coverage" depends="jar,instrument,cover-test,coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>
  <target name="clean" description="delete all intermediary files">
    <delete dir="${classes.dir}" quiet="true" />
    <delete dir="${instrumented.dir}" quiet="true"/>
    <delete dir="${reports.xml.dir}" quiet="true" />
    <delete dir="${reports.html.dir}" quiet="true" />
    <delete dir="${coverage.xml.dir}" quiet="true" />
    <delete dir="${coverage.html.dir}" quiet="true" />
    <delete dir=".clover" quiet="true"/>
    <delete file="cobertura.log" quiet="true"/>
    <delete file="cobertura.ser" quiet="true" />
  </target>
< /project>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值