当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:13:00  来源:本站整理

操纵Ant+JUnit+Cobertura来实现代码覆盖自动测试[Java编程]

赞助商链接



  本文“操纵Ant+JUnit+Cobertura来实现代码覆盖自动测试[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

<?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>


  以上是“操纵Ant+JUnit+Cobertura来实现代码覆盖自动测试[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • <b>在Eclipse中操纵ANT机动构建Web操纵</b>
  • 操纵Ant+JUnit+Cobertura来实现代码覆盖自动测试
  • 操纵ant编译、运行junit以及查抄编码
  • 操纵Annotation举行反转掌握的两种办法
  • 在Eclipse中轻松操纵Ant
  • 在eclipse中操纵Ant实施JUnit
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .