要验证构建的代码是否符合指定的代码覆盖接受条件,可从 Ant 脚本启用代码覆盖。要执行此操作,您需要:
要将 Ant 脚本配置为启用代码覆盖,请将以下行添加到 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/ 下的所有文件启用代码覆盖数据,但排除 bin 的子目录下名称中包含 Test 的文件以及 bin2/ 下的文件。对经过代码覆盖分析的每个文件都将生成备份,基线文件存储在 blfile.coveragedata 中。
<instrument saveBackups="true" baseLineFile="blfile.coveragedata"> <buildPath> <fileset dir="bin"> <exclude name="**/*Test*"/> </fileset> </buildPath> <buildPath> <pathelement location="bin2"/> </buildPath> </instrument>
在将 Ant 脚本配置为启用代码覆盖之后,就可以从命令行或工作台运行 Ant 脚本。
要从命令行运行 Ant 脚本:
ant -lib=<install_dir>\plugins\org.eclipse.hyades.probekit_<version>\probekit.jar myTarget
要从工作台运行 Ant 脚本:
确保类路径包含以下路径:
<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 运行时报告引擎,并解压缩其内容。设置以下环境变量:
代码覆盖报告任务的格式如下:
<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 脚本代码覆盖结果:
注:您也可以选择一个或多个覆盖数据文件,单击右键,然后选择代码覆盖 > 生成报告...。指定报告的关联项目和文件夹位置,然后单击完成。
要使用 ant 任务禁用已检测的类,请重新编译项目,或者如果启用了 saveBackups,可以将 .class 文件替换为 .class.bak 文件。
相关概念© Copyright IBM Corporation 2007, 2008. All Rights Reserved.