为什么 JaCoCo Maven 插件会跳过报告?

Why is JaCoCo Maven Plugin skipping the report?

我有单元测试,我想在报告中获得代码覆盖率结果。 当我 运行 使用 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>

为什么插件会跳过执行?我该如何解决这个问题?

获得代码覆盖率报告的第一步是启动 JaCoCo 代理,这是通过调用插件的 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.

您当前的配置正确设置了 destFile, that is later used by the report goal and its dataFile 参数。

问题是指向 JaCoCo 代理的 属性 没有被正确使用。您对 JaCoCo Maven 插件的配置将 propertyName 参数设置为 surefireArgLine

<propertyName>surefireArgLine</propertyName>

这意味着 JaCoCo 将在这个 属性 中存储其运行时代理的路径,并且当 Surefire Plugin is invoking the tests, this property will need to be added to the VM arguments. However, the Surefire Plugin does not use the surefireArgLine to add VM arguments during the tests; more specifically, the right parameter is called argLine:

Arbitrary JVM options to set on the command line.

因此,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

请注意,默认情况下需要 none:如前所述,JaCoCo 默认将此 VM 参数存储在 argLine 属性 中,这正是用于注入自定义 VM 参数的 Surefire 插件参数的名称。因此,另一种解决方案是删除您的

<propertyName>surefireArgLine</propertyName>

配置元素,让默认值生效。