29

我无法让 Cobertura 插件在 Maven 中运行集成测试。我找到的与这个问题最接近的答案是http://jira.codehaus.org/browse/MCOBERTURA-86。但是,这个问题仍然是一个开放的错误。我在 09 年 4 月 3 日尝试了 Stevo 建议的配置,但没有成功。

我的 POM

<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.3-SNAPSHOT</version>
            <reportSets>
            <reportSet>
                <reports>
                    <report>cobertura-integration</report>
                </reports>
            </reportSet>
            </reportSets>               
        </plugin>   
    </plugins>
</reporting>

顺便说一句,这与 Stevo 提供的配置片段完全相同。

4

6 回答 6

7

在我看来,cobertura maven 插件有两大缺点。它没有报告唯一的目标,所有单元测试都将再次在万无一失的情况下运行。它只为单元测试创​​建代码覆盖率

我现在正在使用JaCoCo maven 插件。JaCoCo重用确定性和/或故障安全报告来创建单元和/或集成测试的代码覆盖率。此外,JaCoCo 具有良好的 Jenkins 集成。这是 JaCoCo 使用安全单元测试和故障安全集成测试的示例。

    <build>
    <plugins>
        <!-- Unit tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
        </plugin>

        <!-- Integration tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.16</version>
            <executions>
                <execution>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!--
            The JaCoCo plugin generates a report about the test coverage. In contrast to the cobertura plugin
            JaCoCo can be configured to generate code coverage for integration tests. Another advantage of JaCoCo
            is that it reports only, cobertura executes all unit tests again (beside the surefire plugin).
        -->
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.6.4.201312101107</version>
            <executions>
                <execution>
                    <id>jacoco-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-prepare-agent-integration</id>
                    <goals>
                        <goal>prepare-agent-integration</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-report</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-integration</id>
                    <goals>
                        <goal>report-integration</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules />
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
于 2013-12-19T13:20:10.630 回答
3

尝试为该插件配置执行阶段,例如

 <build>
   <plugins>
     <plugin>
       <groupId>org.codehaus.mojo</groupId>
         <artifactId>cobertura-maven-plugin</artifactId>
         <version>2.5.1</version>
         <configuration>
           <formats>
             <format>xml</format>
           </formats>
         </configuration>
         <executions>
           <execution>
             <id>cobertura-check</id>
             <phase>verify</phase>
             <goals>
               <goal>cobertura</goal>
             </goals>
           </execution>
         </executions>
       </plugin>

这样它对我来说就像一个魅力。

于 2011-08-15T12:40:49.410 回答
2

经过一些研究发现从http://jira.codehaus.org/browse/MCOBERTURA-86列出的工作配置 确保使用

 mvn clean deploy -PbuildWithIntegrationTestCoverage

        <profile>
        <!-- Build with IntegrationTestcoverage => instrument classes with cobertura before integrationtests starts. -->
        <id>buildWithIntegrationTestCoverage</id>
        <activation>
            <property>
                <name>buildWithIntegrationTestCoverage</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>cobertura-maven-plugin</artifactId>
                    <version>2.5.2</version>
                    <executions>
                        <execution>
                            <id>instrument-classes</id>
                            <phase>package</phase>
                            <goals>
                                <goal>instrument</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- Add cobertura as dependency to the jetty-plugin (required for instrumented classes) -->
                <plugin>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>maven-jetty-plugin</artifactId>
                    <dependencies>
                        <dependency>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>cobertura-maven-plugin</artifactId>
                            <version>2.5.2</version>
                            <type>jar</type>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
于 2012-10-01T19:15:07.817 回答
2

对于通过 Google 遇到这个问题的任何人 - cobertura-maven-plugin开始支持 2.7 版本中的集成测试,该版本于 2015 年发布。

引用他们的网站:

直到 2.6 版,只有一个报告可用:cobertura,它为您的单元测试提供了覆盖率报告。由于只有一个,因此您不必以任何方式对其进行配置。

从 2.7 版开始,添加了一个新报告:cobertura-integration-test,它为您提供集成测试的覆盖率报告。[..]

默认情况下启用这两个报告。如果您希望旧行为仅包含单元测试的覆盖率报告,则需要配置插件...

如果您像我一样使用 运行 cobertura 报告mvn cobertura:cobertura,您还需要进行mvn cobertura:cobertura-integration-test集成测试。您可以从其手册页查看详细信息。

于 2016-12-28T14:54:25.793 回答
0

我发现将 maven-antrun-plugin 用于包括集成和 UI 测试在内的多模块项目是最好的解决方案。我使用这篇文章来降低 Ant 目标并从那里开始。我们现在每个模块都有代码覆盖率报告,并合并了一份包含所有测试覆盖率的报告。

于 2014-04-09T06:51:03.900 回答
-2

通过将 maven-failsafe-plugin 配置为在该test阶段运行,我得到了使用 Maven 3.0.3 和 cobertura-maven-plugin 2.5.1 的集成测试:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.12</version>
    <executions>
        <execution>
            <phase>test</phase>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
于 2012-02-17T20:50:23.907 回答