我如何使jacoco将测试文件本身添加到覆盖率报告中 [英] how do I make jacoco add the tests files themselves to the coverage report

查看:156
本文介绍了我如何使jacoco将测试文件本身添加到覆盖率报告中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的示例maven项目中,我具有以下jacoco配置:

In my sample maven project, I have this jacoco configuration:

<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/(然后更改为最新版本)

which I got from https://automationrhapsody.com/automated-code-coverage-of-unit-tests-with-jacoco-and-maven/ (and then changed to the newest version)

它对实现的覆盖范围(src/main)很有用,但没有为我提供测试本身的覆盖范围信息(src/test)

It works great for the coverage of the implementation (src/main), but doesn't give me any coverage information for the tests themselves (src/test)

尽管我同意这是一个明智的默认设置,但我还是想将其更改为我的一个项目,以告诉我测试的覆盖率信息.有人知道吗?

Although I agree that this is a sensible default, I would like to change it it one of my projects to tell me the coverage information for the tests as well. Does anybody know how?

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

I have a full example here. https://github.com/alex028502/jacoco-example

推荐答案

根据 https://github.com/jacoco/jacoco/issues/271 到今天为止,jacoco-maven-plugin尚未提供此功能,但是

According to https://github.com/jacoco/jacoco/issues/271 as of today this feature is not provided by jacoco-maven-plugin, however one of comments in this ticket also states

可以通过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>

为测试生成以下报告

这篇关于我如何使jacoco将测试文件本身添加到覆盖率报告中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆