代码之家  ›  专栏  ›  技术社区  ›  ReneS

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

  •  51
  • ReneS  · 技术社区  · 16 年前

    有没有一种方法可以将代码排除在cobertura覆盖率报告中?我们有一些方法不应该包括在覆盖率报告中,因此不会降低覆盖率数字。

    我知道三叶草有这样的功能,但我没有发现任何类似的科贝图拉。

    5 回复  |  直到 6 年前
        1
  •  55
  •   Arnost Valicek    9 年前

    可以从检测中排除类。那么它们就不应该出现在报告上。见 排除 陈述如下。

    您还可以忽略对某些方法的调用。见 忽视 陈述如下。

    如果你在使用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>
    
        2
  •  21
  •   Iker Jimenez    13 年前

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

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

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

        3
  •  17
  •   Sarah Phillips    12 年前

    记住也要排除内部类。

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

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

        4
  •  4
  •   Jason    14 年前

    Cobertura目前没有提供这样的功能,Emma(我们使用的)也没有,尽管它被列为一个即将到来的增强——尽管它是对排除规则的扩展,我相信它不是一个注释。

    将方便地掩盖这些难以接近的角落干净,这样你就可以争取100%而不是荒谬。

    我认为注释可能是一种更友好的方式,但它们应该被明确命名,并基于可接受的场景列表,因为我担心像“@excludefromcoverage”这样的东西会被大量添加。

        5
  •  0
  •   Salvioner datowlcs    6 年前

    从2.0开始,你可以自己写 @CoverageIgnore 注释。

    它将被cobertura识别,这将避免考虑注释 方法 (据我所知不在班上工作)。

    1. 创建空批注:
    public @interface CoverageIgnore {}
    
    1. 然后注释要从报告中排除的方法:
    public class SomeClass {
        @CoverageIgnore
        public void foo(String baz) {
            // useless stuff
        }
    }
    

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