簡體   English   中英

使用Cobertura從代碼覆蓋中排除方法

[英]Exclude methods from code coverage with Cobertura

有沒有辦法將代碼排除在Cobertura覆蓋率報告之外? 我們有一些方法不應包含在覆蓋率報告中,因此不會降低覆蓋率數字。

我知道Clover有這樣的功能,但我沒有找到類似的Cobertura。

您可以從檢測中排除類。 然后它們不應出現在報告中。 請參閱以下排除聲明。

您也可以忽略對某些方法的調用。 請參閱以下忽略聲明。

如果您使用的是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>

而對於螞蟻看到這一點

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

這已經讓我頭疼了一段時間了。

我的問題是我在報告部分而不是構建部分設置了cobertura maven插件。

如果您沒有在構建部分進行設置,則不會應用檢測設置,因此不會應用類或包的排除,因此請注意這一點。

記得也要排除內部類。

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

我花了很長時間才注意到我錯過了一個星號!

Cobertura目前沒有提供這樣的功能,Emma(我們使用)也沒有,盡管它被列為即將推出的增強功能 - 盡管我以相信排除規則的形式而不是作為注釋。

將干凈利落地覆蓋那幾個難以接近的角落是很方便的,這樣你就可以100%而不是荒謬地爭取。

我認為注釋可能是一種更友好的方式,但是它們應該被明確地命名並且基於可接受的場景列表,因為我擔心否則會像'@ExcludeFromCoverage'那樣慷慨地添加。

從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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM