Java 科贝图拉仪器
我正试图让Cobertura与我的Ant构建一起工作,特别是希望它能给我一份关于单元测试的覆盖率报告。我正在使用以下目录结构:Java 科贝图拉仪器,java,ant,code-coverage,cobertura,Java,Ant,Code Coverage,Cobertura,我正试图让Cobertura与我的Ant构建一起工作,特别是希望它能给我一份关于单元测试的覆盖率报告。我正在使用以下目录结构: src/main/java --> main source root src/test/java --> test source root bin/main --> where main source compiles to bin/test --> where test source compiles to gen/cobertura --&g
src/main/java --> main source root
src/test/java --> test source root
bin/main --> where main source compiles to
bin/test --> where test source compiles to
gen/cobertura --> cobertura root
gen/cobertura/instrumented --> where "instrumented" class will be copied to
我对Cobertura(,如果我错了,请纠正我!!)的理解是,它将字节码添加到编译类(也称为“插装”)中,然后基于注入/编织的字节码运行报告
因此,我的问题是,如果Cobertura更改了其插入的类的字节码,我应该在
之前还是之后在测试源上运行JUnit,以及为什么?正确的是,Cobertura插入了编译类的字节码。您通常希望从覆盖率分析中排除测试源,因为测试类实际上是生成覆盖率的驱动程序。Cobertura提供的基本示例build.xml在调用Cobertura工具时提供了一个很好的示例:
<cobertura-instrument todir="${instrumented.dir}">
<!--
The following line causes instrument to ignore any
source line containing a reference to log4j, for the
purposes of coverage reporting.
-->
<ignore regex="org.apache.log4j.*" />
<fileset dir="${classes.dir}">
<!--
Instrument all the application classes, but
don't instrument the test classes.
-->
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
</cobertura-instrument>
</target>
这里的exclude元素将所有名称中带有“Test”的类排除在检测之外。下面是一个工作示例,说明如何将Cobertura ANT任务与Junit结合使用以生成代码覆盖率报告
谢谢你的回答,加雷斯!我最初的想法是对我的测试类进行测试,以了解它们“覆盖”了我的主要源代码多少。所以我想我很困惑,插装主类的好处是什么,它们与插装测试类有何不同?代码覆盖率通常用于指示应用程序的测试情况。如果您有100%的覆盖率(这在现实生活中很难实现,因为您必须生成所有可能的错误条件),那么您就知道您的测试覆盖了所有被测试的代码。您也可以插入测试代码,但这只会显示您正在执行的测试代码的比例。维基百科关于代码覆盖率的条目有一个非常好的概述。