2008-09-18 67 views
3

我們有一個多項目,我們正在嘗試運行Cobertura測試報告作爲我們mvn站點構建的一部分。我可以讓Cobertura運行在子項目上,但是它錯誤地報告了0%的覆蓋率,儘管報告仍然突出顯示了單元測試打擊的代碼行。Maven2多項目Cobertura在mvn站點構建過程中報告問題

我們使用的是mvn 2.0.8。我試過運行mvn clean site,mvn clean site:stagemvn clean package site。我知道測試正在運行,它們在肯定的報告中顯示(包括txt/xml和站點報告)。我在配置中丟失了什麼嗎? Cobertura不適用於多項目嗎?

這是在父.pom:

<build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>cobertura-maven-plugin</artifactId> 
       <inherited>true</inherited> 
       <executions> 
        <execution> 
         <id>clean</id> 
         <goals> 
          <goal>clean</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </pluginManagement> 
</build> 
<reporting> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>cobertura-maven-plugin</artifactId> 
      <inherited>true</inherited> 
     </plugin> 
    </plugins> 
</reporting> 

我試着和沒有孩子.poms以下運行它:

<reporting> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>cobertura-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</reporting> 

我的輸出得到這個編譯:

... 
[INFO] [cobertura:instrument] 
[INFO] Cobertura 1.9 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file 
Instrumenting 3 files to C:\workspaces\sandbox\CommonJsf\target\generated-classes\cobertura 
Cobertura: Saved information on 3 classes. 
Instrument time: 186ms 

[INFO] Instrumentation was successful. 
... 
[INFO] Generating "Cobertura Test Coverage" report. 
[INFO] Cobertura 1.9 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file 
Cobertura: Loaded information on 3 classes. 
Report time: 481ms 

[INFO] Cobertura Report generation was successful. 

,報告如下: cobertura report http://trandem.com/images/cobertura.png

+4

男人,你需要一個新的顯示器!我幾乎讀不出那些類名。 – 2008-09-18 23:27:41

回答

1

我懷疑你在編譯階段錯過了cobertura插件的執行,所以代碼只能在測試運行後的站點生命週期中由報告插件檢測。所以測試運行不會被拾取,因爲它們運行在非測試代碼上。更仔細地分析你的構建日誌 - 如果我是對的,你會注意到在cobertura:instrument之前執行的確實測試。

我的配置是與你相似,但除了規定在pluginManagement(喜歡你)的清潔exectution,我明確指定在構建插件部分的Cobertura插件:

<build> 
    ... 
    <plugins> 
    ... 
     <plugin> 
     <inherited>true</inherited> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>cobertura-maven-plugin</artifactId> 
     <version>${cobertura.plugin.version}</version> 
     </plugin> 
    </plugins> 
    </build> 

我的配置八九不離十工程,以及所有Cobertura的東西都在全球組織範圍內,所有項目都用作父母。

通過這種方式,項目不會在其pom.xml中指定與Cobertura相關的任何內容,但它們仍會生成覆蓋率報告。

1

我還沒有成功讓Cobertura結合來自多個項目的報告。對於多項目報告來說,這一直是個問題。

我們一直在評估sonar作爲我們度量報告的解決方案。它似乎在跨項目提供摘要指標方面做得很好,包括多個項目。

1

由我實施的解決方案有點手動,但工程。它由幾個步驟組成,它是將Cobertura生成的幾個.ser文件組合在一起的一個步驟。這可以通過在maven任務中使用cobertura-merge命令行工具來完成。

根據您顯示的輸出是文件沒有實際檢測,它告訴只有3個文件被檢測。

+0

你說得對。本頁展示了這個設置:http://thomassundberg.wordpress.com/2012/02/18/test-coverage-in-a-multi-module-maven-project/。 Ant用於調用cobertura合併,因爲沒有maven目標來調用合併。 – Snicolas 2013-05-11 05:57:03

1

@Marco是正確的,這是不可能通過maven正常實現,只是因爲maven cobertura插件缺少合併目標。

您可以通過Maven和螞蟻目標的混合實現它:http://thomassundberg.wordpress.com/2012/02/18/test-coverage-in-a-multi-module-maven-project/

然而,在情況下,你有一個單一的項目undertest,沒有需要合併。您可以在測試項目中複製測試項目中的.ser文件和儀器類:

//in test project 
<plugin> 
<groupId>com.github.goldin</groupId> 
<artifactId>copy-maven-plugin</artifactId> 
<version>0.2.5</version> 
<executions> 
    <execution> 
    <id>copy-cobertura-data-from-project-under-test</id> 
    <phase>compile</phase> 
    <goals> 
     <goal>copy</goal> 
    </goals> 
    <configuration> 
    <resources> 
     <resource> 
         <directory>${project.basedir}/../<project-under-test>/target/cobertura</directory> 
          <targetPath>${project.basedir}/target/cobertura</targetPath> 
       <includes>     
           <include>*.ser</include> 
       </includes> 
      </resource> 
      <resource> 
        <directory>${project.basedir}/../<project-under-test>/target/generated-classes/cobertura/</directory> 
        <targetPath>${project.basedir}/target/generated-classes/cobertura</targetPath> 
        <preservePath>true</preservePath> 
      </resource> 
     </resources> 
      </configuration> 
     </execution> 
</executions> 
</plugin> 

//in parent project 
<build> 
<plugins> 
    <plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>cobertura-maven-plugin</artifactId> 
    <configuration> 
     <format>xml</format> 
     <aggregate>true</aggregate> 
    </configuration> 
    <executions> 
     <execution> 
        <goals> 
       <goal>clean</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 
    </plugins> 
</build> 
<reporting> 
<plugins> 
    <plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>cobertura-maven-plugin</artifactId> 
    <version>${cobertura.version}</version> 
     </plugin> 
</plugins> 
</reporting>