【问题标题】:Sonar: measuring code coverage when integration tests are in a different projectSonar:在不同项目中进行集成测试时测量代码覆盖率
【发布时间】:2013-08-09 11:28:15
【问题描述】:

具有以下 Maven 项目结构:

-project1          <-- parent pom with two children.
|--module1         <-- web services
\--module1-itest   <-- integration tests written with TestNG

我们今天做什么:

  • 在 module1 中运行 mvn sonar:sonar,显示代码覆盖率 Sonar 仪表板中的单元测试。
  • 在 module1 上运行 mvn jetty:run,然后立即在 module1-itests 上运行 mvn test 进行测试。

我知道这远非理想情况...这更像是一个中间步骤,而我们试图改进一个几乎没有测试的遗留项目...


我的问题是:通过在 Sonar 的模块 1 仪表板中执行集成测试来完成代码覆盖率的最佳方法是什么?

最初,我倾向于将 module1-itest 中的代码移动到 module1,并使用 Failsafe 插件和与 JaCoCo 的有据可查的集成来运行它们。这样,Module1 将混合 JUnit 单元测试和 TestNG 集成测试,每个组分别由 Surefire 和 Failsafe 运行,在预集成阶段启动 Jetty 服务器。

但是,我们有理由将两个项目分开,所以我想知道:

  1. 上面的方法好不好?
  2. 我们是否可以使用任何其他推荐的方法来分隔两个项目,但包括 module1-itestmodule1 Sonar 的仪表板中完成的代码覆盖?

谢谢,

【问题讨论】:

  • 你用的是什么覆盖引擎?
  • 我们正在使用 Emma(在 Eclipse 和 Sonar 中)。但是,如果需要,我们可以切换到 Clover 或 JaCoCo。
  • project1 是父元素(意味着它被模块引用为父元素)和聚合器(它有一个&lt;modules&gt;)元素吗?
  • 是的。如果需要,我们可以对此进行更改。

标签: maven integration-testing code-coverage sonarqube test-coverage


【解决方案1】:

我们就是这样解决的:

总结

  • Service 和 Service-itest 项目是两个独立的模块。
  • 服务具有单元测试,Sonar 报告覆盖率。
  • Service-itests 使用 Cargo 加载服务 WAR 并运行集成测试。在此执行中收集的代码覆盖率在上一级报告。通过这种方式,ITests 的覆盖率被收集并跨模块合并,并在父项目 pom 级别报告。我们也喜欢这种方法,因为它允许我们拥有不同的 ITests 项目(例如,一个使用 Cargo 加载应用程序,另一个使用 JBehave),并且它们可以独立发展。

在父 pom 中:

    <modules>
            <module>service</module>
            <module></module>
            <module>service-itest</module>
        </modules>
    <!-- Sonar -->
            <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
            <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
            <!-- The destination file for the code coverage report has to be set to 
                the same value in the parent pom and in each module pom. Then JaCoCo will 
                add up information in the same report, so that, it will give the cross-module 
                code coverage. -->
            <sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-itests.exec</sonar.jacoco.itReportPath>
...
<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.6.3.201306030806</version>
                </plugin>
            </plugins>
        </pluginManagement>
...
<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>our.project.packages.*</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>pre-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

并且在-itest项目的pom.xml文件中:

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <configuration>
                    <!-- The destination file for the code coverage report has to be set 
                        to the same value in the parent pom and in each module pom. Then JaCoCo will 
                        add up information in the same report, so that, it will give the cross-module 
                        code coverage. -->
                    <destFile>${project.basedir}/../target/jacoco-itests.exec</destFile>
                    <includes>
                        <include>our.packages.*</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>post-test</id>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.2.2</version>
                <configuration>
                    <skip>${cargo.skip}</skip>
                    <container>
                        <containerId>jetty7x</containerId>
                        <artifactInstaller>
                            <groupId>org.eclipse.jetty</groupId>
                            <artifactId>jetty-distribution</artifactId>
                            <version>7.6.12.v20130726</version>
                            <type>zip</type>
                        </artifactInstaller>
                    </container>
                    <configuration>
                        <type>standalone</type>
                        <properties>
                            <cargo.servlet.port>${jetty.port}</cargo.servlet.port>
                            <cargo.jvmargs>${argLine}</cargo.jvmargs>
                        </properties>
                        <deployables>
                            <deployable>
                                <artifactId>pam_filetask_manager_service</artifactId>
                                <groupId>${project.groupId}</groupId>
                                <type>war</type>
                                <pingURL>http://server:22000/ping</pingURL>
                                <properties>
                                </properties>
                            </deployable>
                        </deployables>
                    </configuration>
                </configuration>
                <executions>
                    <execution>
                        <id>start-server</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>install</goal>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-server</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2016-01-16
    • 2017-11-18
    • 2023-02-16
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多