我有一个家项目与4个模块-3包含码和一些测试(测试等和哈希码的类别),而第4模块是用于测试的其他3个模块。

现在我要运行cobertura代码复盖率的工具,以获得一个概述,这类经过良好测试的和哪些不是。我做了一些调查这一专题似乎cobertura不知道产生正确的代码复盖范围百分比和路线复盖范围,如果某些资料来源是测试都位于其他模块。

我读过一些链接喜欢 SeamTestCoverageWithCobertura使用的插件复盖范围内的一个多模块专家2 但是,必须有一个出的解决方案。任何人都可以报告一些新方向上的这个话题?或是有bether工具,如cobertura?我偶然发现了艾玛,但该工具并不提供线复盖...

有帮助吗?

解决方案

为2.6版,还有一个总的选项,可以设置为真正的父pom:

<reporting>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <outputDirectory>./target/tmpCobertura</outputDirectory>
        <formats>
            <format>html</format>
        </formats>
        <aggregate>true</aggregate>
    </configuration>
  </plugin>
</plugins>
</reporting>

其他提示

我们没有声纳在这里和现在,我们不能安装。所以我不得不找到一个解决办法,并得到了一个。这个方案与一个简单的 mvn clean install -DrunCobertura=true 在一个多模块项目。你只需要增加这档给你的 super pom.xml 项目定义 working.dir 财产以及它应工作。

<profile>
    <id>runCobertura</id>
    <activation>
        <property>
            <name>runCobertura</name>
            <value>true</value>
        </property>
    </activation>
    <properties>
        <cobertura.format>html</cobertura.format>
        <cobertura.working.dir>${working.dir}/${project.version}/cobertura</cobertura.working.dir>
        <cobertura.complete.ser.file>${cobertura.working.dir}/complete.ser</cobertura.complete.ser.file>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.4.1</version>
                <inherited>false</inherited>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>.</directory>
                            <includes>
                                <include>cobertura.ser</include>
                            </includes>
                        </fileset>
                        <fileset>
                                <directory>${cobertura.working.dir}</directory>
                            </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>cobertura-Instrument</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <taskdef resource="tasks.properties"/>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                <if>
                                    <available file="${project.build.outputDirectory}"/>
                                    <then>
                                        <cobertura-instrument>
                                            <fileset dir="${project.build.outputDirectory}">
                                                <include name="**/*.class"/>
                                            </fileset>
                                        </cobertura-instrument>
                                    </then>
                                </if>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>cobertura-createCombinedSerFile</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <taskdef resource="tasks.properties"/>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                <if>
                                    <available file="${cobertura.complete.ser.file}"/>
                                    <then>
                                        <cobertura-merge datafile="${basedir}/tmp.ser">
                                            <fileset file="${cobertura.complete.ser.file}"/>
                                            <fileset file="${basedir}/cobertura.ser"/>
                                        </cobertura-merge>
                                        <move file="${basedir}/tmp.ser" tofile="${basedir}/cobertura.ser"/>
                                    </then>
                                </if>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>cobertura-copyResultSerFileAndSources</id>
                        <phase>test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <taskdef resource="tasks.properties"/>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                <if>
                                    <available file="${basedir}/cobertura.ser"/>
                                    <then>
                                        <move file="${basedir}/cobertura.ser" tofile="${cobertura.complete.ser.file}"/>
                                        <mkdir dir="${cobertura.working.dir}/source"/>
                                        <if>
                                            <available file="${basedir}/src/main/java"/>
                                            <then>
                                                <copy todir="${cobertura.working.dir}/source">
                                                    <fileset dir="src/main/java">
                                                        <include name="**/*.java"/>
                                                    </fileset>
                                                </copy>
                                            </then>
                                        </if>
                                        <cobertura-report datafile="${cobertura.complete.ser.file}" format="${cobertura.format}" destdir="${cobertura.working.dir}/report">
                                            <fileset dir="${cobertura.working.dir}/source"/>
                                        </cobertura-report>
                                    </then>
                                </if>
                            </target>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>net.sourceforge.cobertura</groupId>
                        <artifactId>cobertura</artifactId>
                        <version>1.9.4.1</version>
                    </dependency>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>20020829</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>net.sourceforge.cobertura</groupId>
            <artifactId>cobertura</artifactId>
            <version>1.9.4.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</profile>

这是什么做的:

1. @process-classes-仪器的编制课程的模块。

2. @generate-test-sources-合并的 .ser 文件从以前的模块与创建了这个模块,以获得完整的代码复盖范围。

3. @test-创建代码复盖的报告。应该呼吁在最后一个模块,但是由于事实上,在过去的模块可以改变,我呼吁它总是和以前的报告将复盖。如果您使用的报告 xml 格式(为詹金斯)很快,因此它没有关系。

根据 MCOBERTURA-65, ,家cobertura插件仍然不知道如何综合报告的子模块进入一个综合的一种。一些工作已经完成,以实现一个 merge 目标在家cobertura插件(见 MCOBERTURA-33)但是,这代码没有被包括在插件。我没有试修补自己并不能说它是否值得一试。

因此,许多人实际上建议使用的家 仪表板插件 但我会亲自远离它,因为它不是非常令人满意的长期和我所面临的许多问题(技术问题,丢失的历史,...).相反, 我热烈推荐 声纳.看看 尼莫, 公共实例的最后版本的声纳,对于现场演示这一工具。例如,见的 下议院消化器 项目 钻下所复盖的代码.

有几个插件,总Cobertura(和其他)的报告。检查出来的 声纳XRadar 插件。还有 仪表板插件, 但这是一个有点笨重。

总的来说,艾玛不会做的 线复盖范围.

我真的很感谢斯Oppermann提交他的runCobertura概的解决方案。这个帮助 我解决问题'你怎么得到的聚合体的复盖范围的报告的多模块项目的时候,你可能不会 能够使用声纳。

我已经创建了一个例子,它说明如何建立多模块项目产生的代码复盖面报告,评估,不仅单元测试的复盖面(在所有子系统),但也复盖面上的集成试验带来的 你的应用程序。战争在码头。例是举办这里:

        http://dl.dropbox.com/u/9940067/code/multi-module-cobertura.zip 

食谱我提供很可重复使用的如果你复制runCobertura配置文件列出如下(基于一个 提供由斯文。)

这里有一些注意事项,这将有助于使用这一资料:

  • 集成测试模块的启动码头(和定义试验运行的反对 生产。战争)必须被命名为网络的试验驾驶员为代码的复盖范围,或你 必须修改中发言runCobertura配置块。

  • 你的复盖范围的报告将会出现,无论你变

  • 你必须包括"干净"的命令行时运行建立代码的复盖范围。"干净"的会吹走之前cobertura.ser文件, 如果左潜伏在可能会导致非常令人困惑的报告 产生(一个标志,你需要"清洁"是,报告显示 100%复盖的一切,包括东西你知道是从来没有所谓。

      mvn -PrunCobertura clean install      # gives you the aggregate reports.
    
  • 该模块网络的试验驾驶员为代码复盖率定义servlet方面的听众,明确地冲cobertura指标盘 当网络服务器将关闭。所谓的容器是应该做这种自动的,但是,没有为我工作,所以 我得挂钩的明确呼吁冲洗出的指标。

  • 一体化测试都做在常规,因为我根本上的一些家项目的骷髅,已经使用常规。对不起加入混乱,但是它不会告诉你怎么做你的测试在时髦的(这是高度推荐。)

  • 请注意,在编纂与runCobertura档所有的文物创建cobertura仪器仪表,即便你 .战争的文件。你永远都不想让这出去在生产过程(一件事就会运行realllll缓慢。) 我还没有 但找到了一种食物的方式获得的文物重新命名自己的'cobertura性是显而易见的。

    <profiles>
    <profile>
        <id>runCobertura</id>
        <activation>
            <property>
                <name>runCobertura</name>
                <value>true</value>
            </property>
        </activation>
        <properties>
            <cobertura.format>html</cobertura.format>
            <working.dir>/tmp</working.dir>
            <cobertura.working.dir>${working.dir}/${project.version}/cobertura</cobertura.working.dir>
            <cobertura.complete.ser.file>${cobertura.working.dir}/complete.ser</cobertura.complete.ser.file>
    
            <!-- scope which determines whether or not cobertura is included in .war file: overriden here -->
            <cobertura.dependency.scope>compile</cobertura.dependency.scope>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.4.1</version>
                    <inherited>false</inherited>
                    <configuration>
                        <filesets>
                            <fileset>
                                <directory>.</directory>
                                <includes>
                                    <include>**/cobertura.ser</include>
                                </includes>
                            </fileset>
                            <fileset>
                                    <directory>${cobertura.working.dir}</directory>
                                </fileset>
                        </filesets>
                    </configuration>
                </plugin>
    
    
    
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <id>cobertura-Instrument</id>
                            <phase>process-classes</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <taskdef resource="tasks.properties"/>
                                    <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                    <echo message="::PROCESS CLASSES: ${artifactId}"/>
    
                                    <if>
                                      <equals arg1="${artifactId}" arg2="web-test-driver-for-code-coverage" />
                                        <then>
                                            <echo message="::SKIPPING PHASE for integration test"/>
                                        </then>
                                        <else>
                                            <if>
                                                <available file="${project.build.outputDirectory}"/>
                                                <then>
                                                    <echo message="::BEFORE INSTRUMENT"/>
                                                    <cobertura-instrument>
                                                        <fileset dir="${project.build.outputDirectory}">
                                                            <include name="**/*.class"/>
                                                        </fileset>
                                                    </cobertura-instrument>
                                                </then>
                                            </if>
                                        </else>
                                    </if>
    
    
                                </target>
                            </configuration>
                        </execution>
                        <execution>
                            <id>cobertura-createCombinedSerFile</id>
                            <phase>generate-test-sources</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <taskdef resource="tasks.properties"/>
                                    <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                    <echo message=":::generate-test-sources"/>
    
    
                                    <if>
                                      <equals arg1="${artifactId}" arg2="web-test-driver-for-code-coverage" />
                                        <then>
                                            <echo message="::SHORT CIRCUIT COMBINE PHASE for integration test"/>
                                            <echo  message="source - ${cobertura.complete.ser.file} dest - ${basedir}/cobertura.ser"/>
                                            <copy file="${cobertura.complete.ser.file}" tofile="${basedir}/cobertura.ser"/>
                                        </then>
                                        <else>
                                            <if>
                                                <available file="${basedir}/cobertura.ser"/>
                                                <then>
                                                    <echo message="::: Is available ${basedir}/cobertura.ser"/>
                                                </then>
                                            </if>
    
                                            <if>
                                                <available file="${cobertura.complete.ser.file}"/>
                                                <then>
                                                    <echo message="before merge1"/>
                                                    <cobertura-merge datafile="${basedir}/tmp.ser">
                                                        <fileset file="${cobertura.complete.ser.file}"/>
                                                        <fileset file="${basedir}/cobertura.ser"/>
                                                    </cobertura-merge>
                                                    <echo message="move temp.ser to ${basedir}/cobertura.ser"/>
                                                    <move file="${basedir}/tmp.ser" tofile="${basedir}/cobertura.ser"/>
                                                </then>
                                            </if>
                                        </else>
                                    </if>
                                </target>
                            </configuration>
                        </execution>
                        <execution>
                            <id>cobertura-copyResultSerFileAndSources</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <taskdef resource="tasks.properties"/>
                                    <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
    
                                    <echo message=":::copyResultSerFileAndSources -beforeIf"/>
                                    <if>
                                        <available file="${basedir}/cobertura.ser"/>
                                        <then>
                                            <echo message="move1"/>
                                            <move file="${basedir}/cobertura.ser" tofile="${cobertura.complete.ser.file}"/>
                                            <mkdir dir="${cobertura.working.dir}/source"/>
                                            <if>
                                                <available file="${basedir}/src/main/java"/>
                                                <then>
                                                    <copy todir="${cobertura.working.dir}/source">
                                                        <fileset dir="src/main/java">
                                                            <include name="**/*.java"/>
                                                        </fileset>
                                                    </copy>
                                                </then>
                                            </if>
                                            <echo message="runreport"/>
                                            <cobertura-report datafile="${cobertura.complete.ser.file}" format="${cobertura.format}" destdir="${cobertura.working.dir}/report">
                                                <fileset dir="${cobertura.working.dir}/source"/>
                                            </cobertura-report>
                                        </then>
                                    </if>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>net.sourceforge.cobertura</groupId>
                            <artifactId>cobertura</artifactId>
                            <version>1.9.4.1</version>
                        </dependency>
                        <dependency>
                            <groupId>ant-contrib</groupId>
                            <artifactId>ant-contrib</artifactId>
                            <version>20020829</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>net.sourceforge.cobertura</groupId>
                <artifactId>cobertura</artifactId>
                <version>1.9.4.1</version>
            </dependency>
        </dependencies>
    </profile>
    </profiles>
    

托马斯Sundberg提供了一个有趣的解决方案在哪些仪器和测试报告是通过 ant, 但所有的测试和依赖关系的管理通过 mvn.

在这里检查: thomassundberg wordpress

这意味着你必须执行的命令下在父级别在这个顺序:

mvn clean compile
ant instrument
mvn test
ant report

将这些步骤的成 sonar 描述Martijn Stelinga.

测试的复盖范围在多模块项目

我可以实现一些非常类似于你需要什么谢谢你为这个回答: 专家-加依赖伪源

我只是说 <classifier>sources</classifier> 和cobertura包括类的依赖关系。

问候。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top