2011-11-07 71 views
4

我想要在使用cobertura在Jetty上運行我的webapp時獲得覆蓋報告。 我們已經通過使用surefire插件爲單元測試運行cobertura。 我們還配置了用於運行集成測試的故障安全插件。Cobertura和碼頭

我已經(手動)裝備我的戰爭並部署它。

當運行mvn verify與集成測試唯一的配置文件,似乎cobertura工作,因爲我得到所有類型的新的警告在Eclipse控制檯(我從那裏運行碼頭)可能是因爲字節碼被cobertura更改。 但是我沒有看到.ser文件被寫入,即使在碼頭服務器上調用"stop"

運行時我得到一個.ser文件mvn cobertura:cobertura,並在我的webapp的target/site目錄下生成報告。該報告顯示0%的覆蓋率,因爲cobertura:cobertura不運行任何測試。

如何使用失效保護使cobertura運行我的集成測試? 還有其他建議嗎?

感謝, 本

回答

5

我通過使用cobertura-it插件解決了這個問題。它擴展了原始的cobertura插件並允許使用僅限報告的目標。另外,我必須運行兩個單獨的命令(見下文)來測試和生成報告(合併爲1命令不起作用)。這是我的插件配置。

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>cobertura-it-maven-plugin</artifactId> 
    <version>2.5</version> 
    <configuration> 
     <formats> 
      <format>html</format> 
     </formats> 
     <check> 
      <haltOnFailure>false</haltOnFailure> 
     </check> 
    </configuration> 
    <executions> 
     <execution> 
      <id>pre-integration-test</id> 
      <phase>pre-integration-test</phase> 
      <goals> 
       <goal>cobertura</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 
<plugin>    
    <groupId>org.mortbay.jetty</groupId> 
    <artifactId>jetty-maven-plugin</artifactId> 
    <version>7.4.2.v20110526</version> 
    <configuration> 
     <stopKey>aaaaaaaaaaaaa</stopKey> 
     <stopPort>8085</stopPort> 
    </configuration> 
    <executions> 
     <execution> 
      <id>start-jetty</id> 
      <phase>pre-integration-test</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
      <configuration> 
       <scanIntervalSeconds>0</scanIntervalSeconds> 
       <daemon>true</daemon> 
       <classesDirectory>target/generated-classes/cobertura</classesDirectory> 
      </configuration> 
     </execution> 
     <execution> 
      <id>stop-jetty</id> 
      <phase>post-integration-test</phase> 
      <goals> 
       <goal>stop</goal> 
      </goals> 
     </execution> 
    </executions> 
    <dependencies> 
     <dependency> 
      <groupId>net.sourceforge.cobertura</groupId> 
      <artifactId>cobertura</artifactId> 
      <version>1.9.4.1</version> 
     </dependency> 
    </dependencies> 
</plugin> 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-failsafe-plugin</artifactId> 
    <version>2.10</version> 
    <executions> 
     <execution> 
       <id>integration-test</id> 
       <phase>integration-test</phase> 
       <goals> 
        <goal>integration-test</goal> 
       </goals> 
     </execution> 
     <execution> 
      <id>verify</id> 
      <phase>verify</phase> 
      <goals> 
       <goal>verify</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

於是我運行此: mvn clean verify

注意,碼頭停止後,有一定的Cobertura消息:

[INFO] ------------------------------------------------------- 
[INFO] BUILD SUCCESSFUL 
[INFO] ------------------------------------------------------- 
[INFO] Total time: 1 minute 13 seconds 
[INFO] Finished at: Sun Nov 13 12:58:29 ICT 2011 
[INFO] Final Memory: 86M/204M 
[INFO] ------------------------------------------------------- 
2011-11-13 12:58:29.765:WARN::4 threads could not be stopped 
Flushing results... 
Flushing results done 
Cobertura: Loaded information on 342 classes. 
Cobertura: Saved information on 342 classes. 

最後,我用mvn site生成報告。這是我的報告配置

<reporting> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-report-plugin</artifactId> 
      <!-- An error on version 2.8 --> 
      <version>2.7</version> 
      <configuration> 
       <reportsDirectories> 
        <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> 
        <reportsDirectory>${project.build.directory}/failsafe-reports</reportsDirectory> 
       </reportsDirectories> 
      </configuration> 
      <reportSets> 
       <reportSet> 
        <reports> 
         <report>report-only</report> 
        </reports> 
       </reportSet> 
      </reportSets> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-project-info-reports-plugin</artifactId> 
      <version>2.1</version> 
      <configuration> 
       <!-- An error that takes long time to generate this report --> 
       <dependencyLocationsEnabled>false</dependencyLocationsEnabled> 
      </configuration> 
      <reportSets> 
       <reportSet> 
        <reports> 
         <report>dependencies</report> 
        </reports> 
       </reportSet> 
      </reportSets> 
     </plugin> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>cobertura-it-maven-plugin</artifactId> 
      <configuration> 
       <formats> 
        <format>html</format> 
        <format>xml</format> 
       </formats> 
      </configuration> 
      <reportSets> 
       <reportSet> 
        <reports> 
         <report>report-only</report> 
        </reports> 
       </reportSet> 
      </reportSets> 
     </plugin> 
    </plugins> 
</reporting>