有没有办法将代码排除在Cobertura覆盖率报告中?我们有一些方法不应该包含在覆盖率报告中,因此不会降低覆盖率数字。
我知道Clover有这样的功能,但我没有找到类似的Cobertura。
答案 0 :(得分:56)
您可以从检测中排除类。然后它们不应出现在报告中。请参阅下面的排除语句。
您也可以忽略对某些方法的调用。请参阅下面的忽略语句。
如果您使用的是maven,请参阅maven plugin manual。
<configuration>
<instrumentation>
<ignores>
<ignore>com.example.boringcode.*</ignore>
</ignores>
<excludes>
<exclude>com/example/dullcode/**/*.class</exclude>
<exclude>com/example/**/*Test.class</exclude>
</excludes>
</instrumentation>
</configuration>
对于蚂蚁,请参阅this。
<cobertura-instrument todir="${instrumented.dir}">
<ignore regex="org.apache.log4j.*" />
<fileset dir="${classes.dir}">
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
<fileset dir="${jars.dir}">
<include name="my-simple-plugin.jar" />
</fileset>
</cobertura-instrument>
答案 1 :(得分:21)
这已经打破了一段时间了。
我的问题是我在报告部分而不是构建部分设置了cobertura maven插件。
如果您没有在构建部分进行设置,则不会应用检测设置,因此不会应用类或包的排除,因此请注意这一点。
答案 2 :(得分:17)
请记住也要排除内部类。
<exclude>path/to/class/MyClass*.class</exclude>
我花了很长时间才注意到我错过了一个星号!
答案 3 :(得分:4)
Cobertura目前没有提供这样的功能,Emma(我们使用的)也没有,尽管它被列为即将推出的增强功能 - 尽管我以相信排除规则的形式而不是作为注释。
将干净利落地遮挡那几个难以接近的角落,这样你就可以在不荒谬的情况下争取100%。
我认为注释可能是一种更友好的方式,但它们应该被明确地命名并基于可接受的场景列表,因为我担心否则会像'@ExcludeFromCoverage'那样慷慨地添加。
答案 4 :(得分:0)
从2.0开始,您可以编写自己的@CoverageIgnore
注释。
这将被Cobertura所认可,它将避免考虑带注释的方法(据我所知,它不适用于类)。
public @interface CoverageIgnore {}
public class SomeClass {
@CoverageIgnore
public void foo(String baz) {
// useless stuff
}
}
来源:https://github.com/cobertura/cobertura/wiki/Coverage-Annotations