使用 Cobertura Maven 插件运行集成测试

发布于 2024-08-19 21:43:51 字数 903 浏览 6 评论 0原文

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

I am having trouble getting the Cobertura plugin to run integration tests in Maven. The closest answer to this question I have found is http://jira.codehaus.org/browse/MCOBERTURA-86. However, the issue remains an open bug. I tried the configuration suggested by Stevo on 03/Apr/09, it didn't work.

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

which is by the way exactly the same as the configuration fragment provided by Stevo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

巴黎夜雨 2024-08-26 21:43:51

在我看来,cobertura maven 插件有两大缺点。它没有仅报告目标,所有单元测试都将再次在 Surefire 旁边运行。它仅为单元测试创​​建代码覆盖率

我现在使用 JaCoCo maven 插件。 JaCoCo 重用可靠和/或故障安全报告来创建单元和/或集成测试的代码覆盖率。此外,JaCoCo 拥有良好的 Jenkins 集成。下面是 JaCoCo 使用 Surefire 单元测试和故障安全集成测试的示例。

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

From my opinion, the cobertura maven plugin has two big disadvantages. It has no report only goal, all unit tests will run beside surefire again. It creates the code coverage for unit tests only .

I am using the JaCoCo maven plugin now. JaCoCo reuses the surefire and/or failsafe reports to create a code coverage from unit and/or integration test. Furthermore JaCoCo has a good Jenkins integration. Here is an example where JaCoCo uses surefire unit tests and failsafe integration tests.

    <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>
你好,陌生人 2024-08-26 21:43:51

尝试为该插件配置一个执行阶段,就像

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

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

Try to configure a execution phase for that plugin like

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

That way it works like a charm for me.

时光病人 2024-08-26 21:43:51

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

After some research found a working config listed from http://jira.codehaus.org/browse/MCOBERTURA-86
Make sure to invoke this with

 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>
私藏温柔 2024-08-26 21:43:51

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

引用他们的网站:

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

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

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

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

For anyone stumbling on this question through Google - cobertura-maven-plugin started supporting integration tests in version 2.7, which was published in 2015.

Quoting their website:

Up to version 2.6 there were only one report available: cobertura,
which gave you a coverage report for your unit tests. Since there was
only one, you didn't have to configure it in any way.

Starting with version 2.7 a new report was added:
cobertura-integration-test, which gives you a coverage report for your
integration tests. [..]

Both reports are enabled by default. If you want the old behaviour
with only a coverage report for your unit tests, you need to configure
the plugin ...

If you, like I did, run cobertura report using mvn cobertura:cobertura, you'll need to do mvn cobertura:cobertura-integration-test to get integration tests as well. You can check the details from its manual page.

樱花细雨 2024-08-26 21:43:51

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

I found that using the maven-antrun-plugin for multi-module projects including integration and UI tests is the best solution. I used this post to get the Ant goals down and went from there. We now have code coverage reports per module and merged one containing coverage from all tests.

百善笑为先 2024-08-26 21:43:51

我通过配置 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>

I got the integration tests working with Maven 3.0.3 and cobertura-maven-plugin 2.5.1 by configuring the maven-failsafe-plugin to run during the test phase:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文