我正在尝试获取集成测试的代码覆盖率报告。 Jacoco maven 插件能够为单元测试提供代码覆盖率,但为集成测试提供 0% 的覆盖率。集成测试正在访问应用程序的其余 api 端点,该应用程序已部署在 tomcat 中。
我的 Maven jacoco 插件和 Surefire 插件看起来像这样。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/target/jacoco-it.exec</dataFile>
<outputDirectory>${project.build.directory}/target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<!-- <skip>true</skip> -->
<!-- <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables> -->
</configuration>
<!-- <configuration> <skip>true</skip> </configuration> -->
<executions>
<execution>
<id>unit-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- Never skip running the tests when the test phase is invoked -->
<!-- <skip>true</skip> -->
<argLine>@{argLine}
-javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse
2016 CI\\JaxRsApp\\target\\jacoco.exec</argLine>
<includes>
<include>**/*UnitTest.java</include>
</includes>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- Never skip running the tests when the integration-test phase
is invoked -->
<!-- argLine>-javaagent:$WORKSPACE/target/lib/jacoco-agent-0.7.9.jar=includes=*,destfile=*/jacoco-coverage.exec,append=false</argLine -->
<skip>false</skip>
<argLine>@{argLine}
-javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse
2016 CI\\JaxRsApp\\target\\jacoco-it.exec
</argLine>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
<excludes>
<exclude>**/*UnitTest.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
我正在测试阶段执行单元测试,并在集成测试阶段执行集成测试。 我收到的最新错误是“由于缺少执行数据文件而跳过 JaCoCo 执行。”
最佳答案
引用documentation of prepare-agent-integration
:
Same as prepare-agent, but provides default values suitable for integration-tests:
- bound to pre-integration-test phase
- different destFile
引用documentation of prepare-agent
:
Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test. Depending on the project packaging type by default a property with the following name is set:
- tycho.testArgLine for packaging type eclipse-test-plugin and
- argLine otherwise.
在大多数情况下,argLine
由启动 JVM 执行单元测试的 maven-surefire-plugin
自动选取 - http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine
argLine: Arbitrary JVM options to set on the command line.
这解释了为什么您会获得单元测试的覆盖。
要获得集成测试的覆盖率,您必须确保此属性传递给被测应用程序的 JVM,即执行 Tomcat 的 JVM,这完全取决于您启动它的方式。
关于java - Jacoco Maven 离线检测 - Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369308/