程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 使用Ant+JUnit+Cobertura來實現代碼覆蓋自動測試

使用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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved