gpt4 book ai didi

java - 如何让 jacoco 将测试文件本身添加到覆盖率报告中

转载 作者:行者123 更新时间:2023-12-02 09:45:53 25 4
gpt4 key购买 nike

在我的示例 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

最佳答案

根据https://github.com/jacoco/jacoco/issues/271截至目前,jacoco-maven-plugin 尚未提供此功能,但是 one of comments in this ticket also states

possible to generate report for test sources by using Ant tasks via 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>

为您的测试生成以下报告

report

关于java - 如何让 jacoco 将测试文件本身添加到覆盖率报告中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56664717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com