【问题标题】:Running integration tests with Cobertura Maven plugin使用 Cobertura Maven 插件运行集成测试
【发布时间】:2011-01-12 09:23:07
【问题描述】:

我无法让 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 提供的配置片段完全相同。

【问题讨论】:

  • 您如何运行您的 IT?使用单独的模块?使用 maven-failsafe-plugin?在单独的个人资料中?
  • 您在哪里放置了集成测试(即 src/it/java 或 src/test/java)以及如何尝试执行测试?

标签: maven-2 integration-testing cobertura


【解决方案1】:

在我看来,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>

【讨论】:

  • 不错的提示。经过一番研究,我还想出了如何使用合并目标创建一个代码覆盖率报告,该报告将单元和集成测试的输出与 JaCoCo 结合起来。
  • 我还没有弄清楚如何让 JaCoco 忽略琐事。你知道怎么做吗?
  • 将 ant 与 maven-antrun-plugin 一起使用,请参阅我的回答。此外,JaCoCo 的 JVM 代理并不总是可行的。停止 JVM(我认为这是报告覆盖率的唯一方法)并不总是您想要或可以做的。 Cobertura 有一个 coberturaFlush.war 来刷新覆盖数据并保持 JVM 运行。
【解决方案2】:

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

 <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>

这样对我来说就像是一种魅力。

【讨论】:

    【解决方案3】:

    经过一些研究发现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>
    

    【讨论】:

      【解决方案4】:

      对于通过 Google 偶然发现此问题的任何人 - cobertura-maven-plugin started supporting integration tests in version 2.7,于 2015 年发布。

      引用他们的网站:

      直到 2.6 版,只有一份报告可用:cobertura, 它为您的单元测试提供了覆盖率报告。既然有 只有一个,您不必以任何方式配置它。

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

      默认情况下会启用这两种报告。如果你想要旧的行为 只有单元测试的覆盖率报告,您需要配置 插件...

      如果您像我一样使用mvn cobertura:cobertura 运行cobertura 报告,您还需要使用mvn cobertura:cobertura-integration-test 来进行集成测试。您可以通过manual page查看详细信息。

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          通过将 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>
          

          【讨论】:

          • 这违背了使用故障安全插件的目的:如果您在测试阶段运行故障安全,您最好将测试作为常规测试(由surefire选择),运行在测试阶段。
          猜你喜欢
          • 2014-10-14
          • 1970-01-01
          • 2011-12-26
          • 1970-01-01
          • 2014-08-30
          • 1970-01-01
          • 2014-12-01
          • 2013-03-11
          • 1970-01-01
          相关资源
          最近更新 更多