Java 使用 Cobertura 从代码覆盖率中排除方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/951569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:29:15  来源:igfitidea点击:

Exclude methods from code coverage with Cobertura

javacode-coveragecobertura

提问by ReneS

Is there a way to exclude code from inclusion into Cobertura coverage reports? We have some methods that should not be included in the coverage report and therefore not drive down the coverage numbers.

有没有办法将代码排除在 Cobertura 覆盖率报告之外?我们有一些方法不应包含在覆盖率报告中,因此不会降低覆盖率。

I know that Clover has such a functionality, but I have not found anything similar for Cobertura.

我知道 Clover 有这样的功能,但我没有发现 Cobertura 有类似的功能。

采纳答案by Juha Syrj?l?

You can exclude classes from instrumentation. Then they should not appear on reports. See excludestatements below.

您可以从检测中排除类。那么它们不应该出现在报告中。请参阅下面的排除语句。

You can also ignore calls to some methods. See ignorestatement below.

您还可以忽略对某些方法的调用。请参阅下面的忽略语句。

If you are using maven, see maven plugin manual.

如果您使用的是 maven,请参阅maven 插件手册

    <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>

And for ant see 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>

回答by Jason

Cobertura doesn't currently provide such a feature, and neither does Emma (which we use) although it is listed as a forthcoming enhancement - although in the form of an extension to the exclusion rules I believe rather than as an annotation.

Cobertura 目前没有提供这样的功能,Emma(我们使用的)也没有提供这样的功能,尽管它被列为即将推出的增强功能 - 尽管我认为是排除规则的扩展形式,而不是作为注释。

Would be handy to cover off those few inaccessible corners cleanly so that you can strive for 100% without being ridiculous.

将那些难以接近的角落干净地覆盖起来会很方便,这样您就可以 100% 努力而不会荒谬。

I think annotations would probably be a friendlier way to do it, but they ought to be fairly explicitly named and based on a list of acceptable scenarios as I fear otherwise something like '@ExcludeFromCoverage' would get added over generously.

我认为注释可能是一种更友好的方法,但它们应该被相当明确地命名并基于可接受的场景列表,因为我担心否则像“@ExcludeFromCoverage”这样的东西会被慷慨地添加。

回答by Iker Jimenez

This has been breaking my head for some time now.

这已经让我头疼了一段时间了。

My problem was that I had the cobertura maven plugin setup in the reporting section instead of the build section.

我的问题是我在报告部分而不是构建部分设置了 cobertura maven 插件。

The instrumentation settings, and hence the excluding of classes or packages, won't be applied if you don't set it up on build section, so watch out for this.

如果您不在构建部分进行设置,则不会应用检测设置,从而排除类或包,因此请注意这一点。

回答by Sarah Phillips

Remember to exclude inner classes too.

记住也要排除内部类。

<exclude>path/to/class/MyClass*.class</exclude>

It took me ages to notice I was missing an asterisk!

我花了很长时间才注意到我遗漏了一个星号!

回答by Salvioner

Since 2.0 you can write your own @CoverageIgnoreannotation.

从 2.0 开始,您可以编写自己的@CoverageIgnore注释。

It will be recognized by Cobertura, which will avoid considering annotated methods(does not work on classes as far as I know).

它将被 Cobertura 识别,这将避免考虑带注释的方法(据我所知,它不适用于类)。

  1. Create an empty annotation:
  1. 创建一个空注释:
public @interface CoverageIgnore {}
  1. Then annotate the methods you want to exclude from the reports:
  1. 然后注释要从报告中排除的方法:
public class SomeClass {
    @CoverageIgnore
    public void foo(String baz) {
        // useless stuff
    }
}

Source: https://github.com/cobertura/cobertura/wiki/Coverage-Annotations

来源:https: //github.com/cobertura/cobertura/wiki/Coverage-Annotations