我的 Jacoco 报告显示代码覆盖率非常少

问题描述 投票:0回答:2

我试图了解 Scala 中的一些单元测试和 Java 中的源代码(测试和代码位于不同的包中)的代码覆盖率。当我运行 Jacoco 报告时,它仅显示 4% 的代码覆盖率。

当我查看报告时,它显示我为其创建测试的许多文件的百分比为 0。我怀疑单元测试未包含在报告中,可能是因为所有测试类文件都位于 target/test-classes 目录中。 我尝试过添加

includeTests
标签,但这没有任何效果。

这是插件在 pom 中的样子:

<plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${jacoco-maven-plugin.version}</version>
                    <configuration>
                        <includeTests>true</includeTests>
                        <!--<includes>
                            <include>**/test-classes/**</include>
                        </includes>-->
                        <excludes>
                            <exclude>**/dto/**</exclude>
                            <exclude>**/exceptions/**</exclude>
                        </excludes>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>report</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

现在我已经注释掉了

include
标签,因为如果我包含它,测试将使用 0 个类进行编译。我还能如何包含 target/test-classes 目录?

抱歉,如果这是一个简单的修复,我对单元测试和 Jacoco 非常陌生。谢谢!

java scala jacoco
2个回答
0
投票

这是我个人的设置。这有点混乱,我仍在研究其中的某些部分,但它对我有用。你可以试试这个。

<plugin>
     <groupId>org.jacoco</groupId>
     <artifactId>jacoco-maven-plugin</artifactId>
     <version>0.8.4</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule>
                                <element>BUNDLE</element>
                                <limits>
                                    <limit>
                                        <counter>COMPLEXITY</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.20</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>

0
投票

可以提供更多的内幕吗?例如。您使用的是哪个版本的 junit?

© www.soinside.com 2019 - 2024. All rights reserved.