若要驗證您建置的程式碼是否符合您指定的程式碼涵蓋面接受準則,可以從 Ant Script 啟用程式碼涵蓋面。為此,您需要:
您也可以在工作台中檢視程式碼涵蓋面結果。
若要配置 Ant Script 來啟用程式碼涵蓋面,請將下列內容新增至 CLASSPATH 環境變數:
<install_dir>/plugins/com.ibm.rational.llc.engine_1.0.0.<version_number>
並在建置檔中定義一項新作業。例如:
<taskdef name="instrument"
classname="com.ibm.rational.llc.engine.instrumentation.anttask.InstrumentationTask"
classpath="${install_dir}/plugins/com.ibm.rational.llc.engine_1.0.0.<version_number>"/>
設備測試作業的格式為:
<instrument buildPath="" baseLineFile="" saveBackups="" outputDir=""/>
其中:
在下列範例中,收集了 bin/ 中所有檔案的程式碼涵蓋面資料,基準線檔案儲存在 blfile.coveragedata 中,未產生這些檔案的備份,而經過設備測試的檔案則儲存在 coverageData_files 中。
<instrument saveBackups="false" baseLineFile="blfile.coveragedata" buildPath="bin/" outputDir="coverageData_files"/>
在下列範例中,收集了 bin 中所有檔案的程式碼涵蓋面資料(參照名稱為 build.Dir),基準線檔案儲存在 blfile.coveragedata 中,並產生了這其中每個檔案的備份。
<path id="build.Dir">
<pathelement location="bin"/>
</path>
<instrument saveBackups="true" baseLineFile="blfile.coveragedata" buildPathRef="build.Dir" />
在下列範例中,收集了 bin/ 中所有檔案的程式碼涵蓋面資料,基準線檔案儲存在 blfile.coveragedata 中,並產生了這其中每個檔案的備份。
<instrument saveBackups="true" baseLineFile="blfile.coveragedata"> <buildPath> <pathelement location="bin"/> </buildPath> </instrument>
在下列範例中,啟用了 bin/ 中目錄名稱包含 com 的所有子目錄的程式碼涵蓋面資料,但排除了名稱包含 Test 的子目錄中的檔案。產生了正在進行程式碼涵蓋面分析的每個檔案的備份,而基準線檔案則儲存在 blfile.coveragedata 中。
<instrument saveBackups="true" baseLineFile="blfile.coveragedata"> <buildPath> <fileset dir="bin"> <exclude name="**/*Test*"/> <include name="**/*com*/*"> </fileset> </buildPath> </instrument>
在下列範例中,啟用了 bin/ 及 bin2/ 中所有檔案(bin 的子目錄中名稱包含 Test 的檔案除外)的程式碼涵蓋面資料。產生了正在進行程式碼涵蓋面分析的每個檔案的備份,而基準線檔案則儲存在 blfile.coveragedata 中。
<instrument saveBackups="true" baseLineFile="blfile.coveragedata"> <buildPath> <fileset dir="bin"> <exclude name="**/*Test*"/> </fileset> </buildPath> <buildPath> <pathelement location="bin2"/> </buildPath> </instrument>
配置了 Ant Script 以啟用程式碼涵蓋面之後,您可以從指令行或工作台執行 Ant Script。
若要從指令行執行 Ant Script:
ant -lib=<install_dir>\plugins\org.eclipse.hyades.probekit_<version>\probekit.jar myTarget
若要從工作台執行 Ant Script:
確保您的類別路徑包含下列內容:
<install_dir>\plugins\com.ibm.rational.llc.engine_1.0.0.<version_number>
若要從指令行產生程式碼涵蓋面統計,請從程式碼的上層目錄執行:
java -Dcoverage.out.file=C:\coverage_report\coverage_data.coveragedata com.ibm.rational.llc.example
附註:您可能需要設定您的 Java 來符合產品安裝的 Java。
若要使用 Ant 產生報告: 下載 BIRT Runtime Report Engine 並解壓縮其內容。 設定下列環境變數:
程式碼涵蓋面報告作業的格式為:
<code-coverage-report outputDir="" coverageDataFile="" baseLineFiles=""> <filters> <filter type="" value=""/> </filters> <configurations> <configuration name="" value=""/> </configurations> </code-coverage-report>
其中:
在下列範例中,使用 coverage-data/blfile.coveragedata 作為基準線檔案,對 coverage-data/coverage.coveragedata 收集了產生的報告,報告儲存在 coverage-reports 中,並套用了臨界值為 80% 的 line_coverage_threshold 過濾器。計算程式碼涵蓋面時會排除預設建構子。
<code-coverage-report outputDir="coverage-reports/" coverageDataFile="coverage-data/coverage.coveragedata" baseLineFiles="coverage-data/blfile.coveragedata"> <filters> <filter type="line_coverage_threshold" value="80"/> </filters> <configurations> <configuration name="excludeDefaultConstructor" value="true"/> </configurations> </code-coverage-report>
在下列範例中,使用 blfile1.coveragedata 和 blfile2.coveragedata 作為基準線檔案,對兩個涵蓋面檔案 coverage1.coveragedata 和 coverage2.coveragedata 收集了產生的報告,報告則儲存在 coverage-reports 中。依預設,在計算程式碼涵蓋面時會包含預設建構子。
<code-coverage-report outputDir="coverage-reports/" coverageDataFile="coverage-data/coverage1.coveragedata;coverage-data/coverage2.coveragedata" baseLineFiles="coverage-data/blfile1.coveragedata;coverage-data/blfile2.coveragedata"> </code-coverage-report>
若要在工作台中檢視 Ant Script 程式碼涵蓋面結果:
附註:您也可以改為選取一個以上涵蓋面資料檔、按一下滑鼠右鍵,然後選取程式碼涵蓋面 > 產生報告...。指定報告的關聯專案以及資料夾位置,然後按一下完成。
若要使用 ant 作業停用經過設備測試的類別,請重新編譯專案,或將 .class 檔案取代為 .class.bak 檔案(若 saveBackups 已啟用)。
相關概念© Copyright IBM Corporation 2007, 2008. All Rights Reserved.