客户端/服务器Web应用程序的代码覆盖率

时间:2013-11-21 18:20:27

标签: java maven code-coverage sonarqube

我正在编写一个多模块应用程序。一些模块只是基本的Java库,然后包含在webapp的WAR中。

我想在以下场景中运行代码覆盖:

  • 我正在通过Maven启动的嵌入式Jetty运行webapp。

  • 我有针对webapp执行HTTP请求的测试。

  • 我想在网络应用程序和测试中获得代码。

这是可能的,如何通过Cobertura,JaCoCo或Emma实现?据我所知,代码覆盖率仅涵盖此方案中的客户端代码。我是对的吗?

4 个答案:

答案 0 :(得分:2)

我认为如果你设法将JaCoCo-agent附加到运行jetty的jvm,它应该能够测量在你对webapp运行集成测试时调用了哪些代码。因此,您应该获得一个显示代码覆盖率的统计信息。

有一个JaCoCo Maven插件 - 虽然我不确定这对你的情况是否有帮助。只是在单元测试中使用它。

编辑:发现一篇博客帖子似乎指向了正确的方向 Measure Code Coverage by Integration Tests with Sonar

答案 1 :(得分:1)

您可以使用Jacoco插件生成代码覆盖率以下是我用于junit测试代码覆盖率的插件配置。

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.5.10.201208310627</version>
                <configuration>
                    <skip>${maven.test.skip}</skip>
                    <output>file</output>
                    <append>true</append>
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

注意:在使用eclipse时,你可能会在maven中获得生命周期未覆盖的错误,一种方法是使用插件管理明确提及生命周期。我从市场安装了jacoco插件,解决了我的问题

答案 2 :(得分:1)

以下是我实现它的方式

假设您已经有一个最小的pom.xml配置:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</
  <version>0.7.4.201502262128</vers
</plugin>
  1. Download JaCoCo's agent并将jacocoagent.jar复制到合适的位置(例如$HOME/tools/jacocoagent0.7.4.jar

  2. 通过以下方式将JaCoCo的代理人附加到Maven的JVM:

    export MAVEN_OPTS="$MAVEN_OPTS \
    -javaagent:$HOME/tools/jacocoagent0.7.4.jar=output=tcpserver,port=6300"
    
  3. 使用嵌入式jetty服务器运行您的应用程序,例如mvn jetty:run

  4. 运行集成测试

  5. 在另一个shell中,通过mvn jacoco:dump jacoco:report

  6. 转储和报告
  7. ./target/site/index.html(默认情况下)

  8. 上打开报告

答案 3 :(得分:1)

我们有类似的情况,在jetty服务器上运行集成测试。此外,我们需要所有测试单元和集成的组合报告。我们实现的解决方案是运行分叉码头并使用jacoco javaagent详细信息传递jvmargs。我们的代码覆盖率报告涵盖了所有其他api和服务层java代码。 jacoco的pom配置

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco-maven-plugin.version}</version>
    <configuration>
        <append>true</append>
    </configuration>
    <executions>
        <execution>
            <id>prepare-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/jacoco.exec</destFile>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>prepare-integration</id>
            <goals>
                <goal>prepare-agent-integration</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/jacoco.exec</destFile>
                <propertyName>failsafeArgLine</propertyName>
            </configuration>
        </execution>
    </executions>
</plugin>

通过上面的配置,我们为单元和集成测试生成了一个通用的exec文件。接下来我们将jetty配置为run-forked

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty-maven-plugin.version}</version>
    <configuration>
        <stopKey>foo</stopKey>
        <stopPort>9999</stopPort>
        <webApp>
            <contextPath>/myway</contextPath>
            <descriptor>src/main/webapp/WEB-INF/web.xml</descriptor>
        </webApp>
        <!-- passing the jacoco plugin as a jvmarg -->
        <jvmArgs>${failsafeArgLine}</jvmArgs>
    </configuration>
    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <configuration>
                <daemon>true</daemon>
                <waitForChild>false</waitForChild>
            </configuration>
            <goals>
                <goal>run-forked</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这将使用jvmargs在一个单独的jvm中启动码头。最后,我们在pom的报告标签中生成了报告。我们注意到将报告添加到构建插件并没有捕获由jetty运行的集成测试。

<reporting>
  </plugins>
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco-maven-plugin.version}</version>
        <reportSets>
            <reportSet>
                <id>jacoco-report</id>
                <reports>
                    <report>report</report>
                </reports>
            </reportSet>
        </reportSets>
      </plugin>
    </plugins>
</reporting>

可以在target / site / jacoco / index.html访问报告,也可以从命令行运行报告。

  

mvn jacoco:报告

希望它有所帮助。

相关问题