jacoco maven插件没有报告测试覆盖率

时间:2014-08-04 02:25:28

标签: java maven jacoco

我正在尝试使用jacoco和maven。当我运行mvn clean test时,我希望将coverage输出写入target / coverage-reports但是当我在构建之后打开index.html时它就是空的。

我检查了以下内容, - jacoco.exec文件存在,它有一堆类名 - 在coverage html中有一个链接' Sessions'当我点击它时,如果我的类似乎已被执行,我会看到一堆 - 我在运行maven命令时看到没有错误或警告

我很困惑为什么报告是空的。从我看到的所有例子看来,这似乎应该有效。我错过了什么?

我正在使用maven插件的以下jacoco配置

  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <configuration>
      <append>true</append>
      <skip>false</skip>
      <excludes/>
      <outputDirectory>${project.build.directory}/coverage-reports/</outputDirectory>
      <dataFile>${project.build.directory}/jacoco.exec</dataFile>
      <includes>
        <include>mypackage.*</include>
      </includes>
      <check>false</check>
    </configuration>
    <executions>
      <execution>
        <id>pre-test</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
      </execution>
      <execution>
        <id>post-test</id>
        <goals>
          <goal>report</goal>
        </goals>
        <phase>test</phase>
      </execution>
    </executions>
  </plugin>

更新

显然这与我提供的包含模式有关。当我删除包含它确实显示我的覆盖范围,但超过我需要。我仍然试图找出用于做正确包含的模式。

1 个答案:

答案 0 :(得分:1)

您还需要将其挂钩到surefire,将其更改为

 <build>
        <plugins>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.6.3.201306030806</version>
            <executions>
              <!-- pre-unit-test execution helps setting up some maven property,
                which will be used later by JaCoCo -->
              <execution>
                <id>pre-unit-test</id>
                <goals>
                  <goal>prepare-agent</goal>
                </goals>
                <configuration>
                  <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                  <!-- passing property which will contains settings for JaCoCo agent.
                    If not specified, then "argLine" would be used for "jar" packaging -->
                  <propertyName>surefireArgLine</propertyName>
                </configuration>
              </execution>
              <!-- report phase setup -->
              <execution>
                <id>post-unit-test</id>
                <phase>test</phase>
                <goals>
                  <goal>report</goal>
                </goals>
                <configuration>
                  <!-- output file with report data. -->
                  <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                  <!-- output directory for the reports. -->
                  <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>

          <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14</version>
            <executions>
              <execution>
                <id>default-test</id>
                <phase>test</phase>
                <goals>
                  <goal>test</goal>
                </goals>
                <configuration>
                  <argLine>${surefireArgLine}</argLine>
                </configuration>
              </execution>
            </executions>
            <configuration>
              <argLine>-XX:MaxPermSize=512m</argLine>
            </configuration>
          </plugin>
        </plugins>
  </build>
相关问题