2008-09-18 29 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>