我有单元测试,我想在报告中获取代码覆盖率结果。 当我使用Maven运行测试时,我在日志中:
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-unit-test) @ che-core-api-git ---
[INFO] Skipping JaCoCo execution due to missing execution data file:C:\dev\eclipse_che_core\platform-api\che-core-api-git\target\coverage-reports\jacoco-ut.exec
这是我POM的相关部分。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for unit tests is created after
unit tests have been run.
-->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
为什么插件会跳过执行?我该如何解决这个问题?
答案 0 :(得分:1)
获得代码覆盖率报告的第一步是启动JaCoCo代理,这是通过调用插件的prepare-agent
目标来完成的。这个目标的目的是:
准备一个指向JaCoCo运行时代理的属性,该属性可以作为VM参数传递给被测试的应用程序。默认情况下,根据项目打包类型设置具有以下名称的属性:
tycho.testArgLine
包装类型eclipse-test-plugin
和argLine
否则。
您当前的配置正确设置了destFile
,后来由report
目标及其dataFile
参数使用。
问题是指向JaCoCo代理的属性未正确使用。您对JaCoCo Maven插件的配置将propertyName
参数设置为surefireArgLine
<propertyName>surefireArgLine</propertyName>
这意味着JaCoCo将在此属性中存储其运行时代理的路径,并且当Surefire Plugin调用测试时,需要将此属性添加到VM参数中。但是,Surefire插件在测试期间不使用surefireArgLine
添加VM参数;更具体地说,正确的参数称为argLine
:
在命令行上设置的任意JVM选项。
因此,JaCoCo设置指向其代理的属性surefireArgLine
在测试期间未使用。要更正此问题,可以配置Surefire插件以考虑此新属性:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
这将告诉Surefire插件在启动测试时使用新属性surefireArgLine
作为VM参数。通过此更改,您将在日志中看到:
[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (post-unit-test) @ test ---
[INFO] Analyzed bundle 'test' with 3 classes
请注意,默认情况下,不需要这样做:如前所述,JaCoCo默认将此VM参数存储在argLine
属性中,这正是用于注入自定义的Surefire插件参数的名称VM参数。因此,另一种解决方案是删除您的
<propertyName>surefireArgLine</propertyName>
配置元素,让默认值启动。