0

在我的示例 Maven 项目中,我有这个 jacoco 配置:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.4</version>
  <executions>
    <execution>
      <id>jacoco-initialize</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>jacoco-report</id>
      <phase>test</phase>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
  </executions>
</plugin>

我从https://automationrhapsody.com/automated-code-coverage-of-unit-tests-with-jacoco-and-maven/获得的(然后更改为最新版本)

它适用于实现的覆盖率(src/main),但没有给我任何测试本身的覆盖率信息(src/test)

尽管我同意这是一个合理的默认设置,但我想将其更改为我的项目之一,以告诉我测试的覆盖率信息。有人知道怎么做吗?

我在这里有一个完整的例子。https://github.com/alex028502/jacoco-example

4

2 回答 2

1

根据https://github.com/jacoco/jacoco/issues/271的说法,截至今天,此功能尚未由 提供jacoco-maven-plugin,但该票证中的一条评论还指出

可以通过使用 Ant 任务为测试源生成报告maven-antrun-plugin

例如

            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-antrun-plugin</artifactId>
              <version>1.8</version>
              <dependencies>
                <dependency>
                  <groupId>org.jacoco</groupId>
                  <artifactId>org.jacoco.ant</artifactId>
                  <classifier>nodeps</classifier>
                  <version>0.8.4</version>
                </dependency>
              </dependencies>
              <executions>
                <execution>
                  <phase>package</phase>
                  <goals>
                    <goal>run</goal>
                  </goals>
                  <configuration>
                    <target>
                      <typedef resource="org/jacoco/ant/antlib.xml"/>
                      <report>
                        <executiondata>
                          <fileset dir="target" includes="jacoco.exec"/>
                        </executiondata>
                        <structure name="Coverage Report">
                          <classfiles>
                            <fileset dir="${basedir}/target/test-classes"/>
                          </classfiles>
                          <sourcefiles>
                            <fileset dir="${basedir}/src/test/java"/>
                          </sourcefiles>
                        </structure>
                        <html destdir="${basedir}/target/coverage-report/html"/>
                      </report>
                    </target>
                  </configuration>
                </execution>
              </executions>
            </plugin>

为您的测试生成以下报告

报告

于 2019-06-19T12:17:34.127 回答
0

此外,我们可以在 jacoco 插件中添加我们希望在项目中使用限制和计数器的最小覆盖范围,或者我们可以允许跳过多少最大类。

遵循插件示例:

          <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-check</id>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>BUNDLE</element>
                                    <limits>
                                        <limit>
                                            <counter>INSTRUCTION</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0</minimum>
                                        </limit>
                                        <limit>
                                            <counter>CLASS</counter>
                                            <value>MISSEDCOUNT</value>
                                            <maximum>50</maximum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
于 2019-06-19T09:59:35.343 回答