首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

整合 Cobertura 與 Tomcat 實現程式碼涵蓋率測試完整指南

     編輯:Java 相關知識

前言

在 Java Web 專案開發中,程式碼涵蓋率是衡量測試品質的關鍵指標。Cobertura 作為一款開源的程式碼涵蓋率工具,能夠偵測測試過程中哪些程式碼被執行、哪些被遺漏。本文將詳細說明如何在 Tomcat 伺服器環境中整合 Cobertura,並透過 Ant 建置腳本自動化整個測試與報表產生流程。

關鍵注意事項

  • 庫檔案部署:必須將 cobertura.jar 及其相依的 lib 目錄下的所有 JAR 檔複製到 Tomcat 的 common/lib 目錄下,確保伺服器啟動時能載入 Cobertura 的類別。
  • 資料寫入時機:Cobertura 只有在 Tomcat 正常關閉(shutdown)時,才會將涵蓋率資料寫入 cobertura.ser 序列化檔案。若強制終止進程,資料將遺失。

標準作業流程

  1. 啟動 Tomcat (startup)
  2. 建置 WAR 檔 (build war)
  3. 部署 Web 應用 (deploy)
  4. 執行 Web 測試 (web test)
  5. 移除部署 (undeploy)
  6. 關閉 Tomcat (shutdown)
  7. 產生涵蓋率報表 (coverage report)

Ant 建置腳本 (build.xml) 完整解析

以下提供一個完整的 build.xml 範例,涵蓋從程式碼檢出、編譯、儀器化、單元測試、系統測試到報表產生的完整生命週期。

專案屬性與路徑定義

<project name="KSystem" default="smoketest" basedir=".">
  <property environment="env"/>
  <property name="checkstyle.dir" value="${env.CHECKSTYLE_HOME}" />
  <path id="checkstyle.classpath">
    <fileset dir="${checkstyle.dir}">
      <include name="checkstyle-all-4.1.jar" />
    </fileset>
  </path>
  <property name="cobertura.dir" value="${env.COBERTURA_HOME}" />
  <path id="cobertura.classpath">
    <fileset dir="${cobertura.dir}">
      <include name="cobertura.jar" />
    </fileset>
  </path>
  <!-- 定義其他任務:jcsc, checkstyle, pmd -->
  <taskdef name="jcsc" classname="rj.tools.jcsc.ant.JCSCTask" classpath="${env.JCSC_HOME}/lib/jcsc.jar" />
  <taskdef resource="checkstyletask.properties" classpathref="checkstyle.classpath" />
  <taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
  <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpath="${env.PMD_HOME}/lib/pmd-3.7.jar"/>

目錄結構與編譯目標

<property name="project.dir" value="${basedir}" />
<property name="project.lib.dir" value="${project.dir}/WEB-INF/lib" />
<property name="build.dir" value="${basedir}/build" />
<property name="out.dir" value="${build.dir}" />
<property name="out.file.name" value="${ant.project.name}" />
<property name="classes.dir" value="${out.dir}/classes" />
<property name="src.dir" value="${project.dir}/src" />
<property name="webtests.dir" value="${project.dir}/webtests" />
<property name="instrumented.dir" value="${out.dir}/instrumented" />

<target name="qa.build" description="build java file including cobertura for coverage test">
  <javac target="1.4" source="1.4" destdir="${classes.dir}" debug="yes" encoding="utf-8">
    <src path="${src.dir}" />
    <src path="${tests.dir}" />
    <include name="**/*.java" />
    <classpath refid="compile.classpath"/>
    <classpath refid="cobertura.classpath" />
  </javac>
  <copy todir="${classes.dir}">
    <fileset dir="${src.dir}">
      <exclude name="**/*.java"/>
    </fileset>
  </copy>
</target>

儀器化與涵蓋率檢查

<target name="instrument">
  <delete file="cobertura.ser"/>
  <delete dir="${instrumented.dir}" />
  <mkdir dir="${instrumented.dir}" />
  <cobertura-instrument todir="${instrumented.dir}">
    <ignore regex="org.apache.log4j.*" />
    <fileset dir="${classes.dir}">
      <include name="**/*.class" />
      <exclude name="**/*Test.class" />
      <exclude name="**/AllTests.class" />
    </fileset>
  </cobertura-instrument>
  <copy todir="${instrumented.dir}">
    <fileset dir="${classes.dir}">
      <exclude name="**/*.class"/>
    </fileset>
  </copy>
</target>

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

<target name="coverage-report">
  <cobertura-report srcdir="${src.dir}" destdir="${reports.cobertura.xml.dir}" format="xml" />
</target>

<target name="alternate-coverage-report">
  <cobertura-report destdir="${reports.cobertura.html.dir}">
    <fileset dir="${src.dir}">
      <include name="**/*.java"/>
    </fileset>
  </cobertura-report>
</target>

Tomcat 整合與系統測試流程

腳本定義了 Tomcat 的啟動、關閉、部署、移除部署任務,並利用 parallel 和 sequential 協調伺服器啟動與測試執行的順序:

<target name="systemtest">
  <property name="tests.dir" value="${project.dir}/webtests" />
  <antcall target="checkout"/>
  <parallel threadCount="2">
    <ant target="tomcat.startup" />
    <sequential>
      <sleep seconds="4"/>
      <antcall target="initdir"/>
      <antcall target="qa.build"/>
      <antcall target="instrument"/>
      <antcall target="buildwar"/>
      <antcall target="tomcat.deploy"/>
      <antcall target="webtest"/>
      <antcall target="tomcat.undeploy"/>
      <antcall target="tomcat.shutdown"/>
      <antcall target="junitreport"/>
      <antcall target="coverage-report"/>
      <antcall target="alternate-coverage-report"/>
      <antcall target="jcscreport"/>
      <antcall target="checkstylereport"/>
      <antcall target="pmd"/>
    </sequential>
  </parallel>
  <delete dir="${tomcat.home}/webapps/${webapp.name}" />
</target>

總結

透過將 Cobertura 整合至 Tomcat 並配合 Ant 自動化腳本,開發團隊能夠在持續整合流程中無縫嵌入程式碼涵蓋率檢測。關鍵在於正確配置類別路徑、確保 Tomcat 正常關閉以持久化資料,以及合理安排儀器化與測試執行的順序。上述 build.xml 提供了可直接參考的企業級配置範本。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved