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

2022-01-17 00:00:00 code-coverage sonarqube java maven

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

I am writing a multi-module application. Some of the modules are just basic Java libraries which are then included in the WAR of a webapp.

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

I would like to run code coverage in the following scenario:

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

  • I am running the webapp through an embedded Jetty that is started via Maven.

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

I have tests which are executing HTTP requests against the webapp.

我想获得包含在 webapp 和测试中的代码.

I would like to get code covered in the webapp and also by the tests.

这可能吗?如何使用 Cobertura、JaCoCo 或 Emma 来实现?据我了解,在这种情况下,代码覆盖率只会覆盖客户端代码.我说的对吗?

Is this possible and how can it be achieved with Cobertura, JaCoCo or Emma? From what I understand, the code coverage will only cover the client-side code in this scenario. Am I correct?

推荐答案

我是这样实现的

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

Assuming you already have a minimal pom.xml config:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</
  <version>0.7.4.201502262128</vers
</plugin>

  1. 下载JaCoCo的代理并将jacocoagent.jar复制到合适的位置(例如$HOME/tools/jacocoagent0.7.4.jar)

  1. Download JaCoCo's agent and copy jacocoagent.jar to a suitable location (e.g. $HOME/tools/jacocoagent0.7.4.jar)

通过以下方式将 JaCoCo 的代理连接到 Maven 的 JVM:

Attach JaCoCo's agent to Maven's JVM via:

export MAVEN_OPTS="$MAVEN_OPTS 
-javaagent:$HOME/tools/jacocoagent0.7.4.jar=output=tcpserver,port=6300"

  • 使用嵌入式码头服务器运行您的应用程序,例如mvn jetty:run

    运行您的集成测试

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

    In another shell, dump and report via mvn jacoco:dump jacoco:report

    ./target/site/index.html 上打开您的报告(默认)

    Open your report on ./target/site/index.html (by default)

  • 相关文章