how do I make jacoco add the tests files themselves to the coverage report

时间秒杀一切 提交于 2019-12-24 19:07:37

问题


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>

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

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?

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


回答1:


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

possible to generate report for test sources by using Ant tasks via maven-antrun-plugin

For example

            <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>

generates following report for your test




回答2:


Also, we can add in the jacoco plugin that how much minimum coverage we want in our project using limit and counter or how many max classes we can allow to skip.

follow sample of plugin:

          <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>


来源:https://stackoverflow.com/questions/56664717/how-do-i-make-jacoco-add-the-tests-files-themselves-to-the-coverage-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!