如何从代码覆盖率中排除某些类?(爪哇) [英] How to exclude certain classes from being included in the code coverage? (Java)

查看:78
本文介绍了如何从代码覆盖率中排除某些类?(爪哇)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经可以通过在我的 POM 文件的标签中添加这个来排除带有声纳立方体报告的类.

I could already exclude the classes with sonar cube report by adding this in the tag of my POM file.

 <properties>
        <java.version>1.8</java.version>
        <sonar.exclusions> 
            /src/main/java/com/example/org/test/mainpackage/message/**/*
            ,
            /src/main/java/com/example/org/test/mainpackage/security/jwt/**/*
            ,
            /src/main/java/com/example/org/test/mainpackage/security/services/**/*
            ,
            /src/main/java/com/example/org/test/mainpackage/controller/AuthRestAPIs.java
            ,
            /src/main/java/com/example/org/test/mainpackage/controller/TestRestAPIs.java
        </sonar.exclusions>
    </properties>

但是,在本地运行以测试代码覆盖率的相同代码根本不起作用.

However the same code when run locally to test the code coverage is not working at all.

我在哪里可以使用 Maven & 以编程方式进行设置?弹簧靴?.

Where do I set this up programmatically with Maven & Spring Boot ?.

我已经可以通过

设置 > 构建、执行、部署 > 排除 > [Class(es) to be排除]

Settings > Build, Execution, Deployment > Excludes > [Class(es) to be excluded]

或通过

Edit Configurations > Select Code Coverage tab > 然后添加我想排除或仅包含在代码中的包或类覆盖率报告.

Edit Configurations > Select Code Coverage tab > then adding the package or class I want to be excluded or include only in the code coverage report.

我想要的是对要排除的类进行编码,即在 pom 文件中,就像我在排除声纳 qube 代码时所做的那样.

What I want is coding the classes to be excluded i.e in the pom file like I did with exclusion of code for sonar qube.

为了简短.我如何以编程方式执行此操作?.

To make it short. How do I do this programmatically?.

我想要以编程方式进行此操作的原因是,我为排除代码覆盖率中报告的文件所做的上述两个步骤是,我的所有项目中都没有包含可以推送到存储库中的文件,以便我对排除项所做的更改将反映在所有将从 git 存储库中提取的人员中.没有添加任何文件或配置,我可以推送并反映到 repo 中的所有内容.这意味着我的排除项仅在本地有效,对其他人无效.正确的?.所以在这里我要求一种以编程方式执行的方法,例如将排除项放入 POM 文件中.

Reason why I wanted to this programmatically is that, the above two steps I did to exclude the files from being reported in the code coverage is that no files was included in all of my project that I could push in the repository so that changes i made for exclusions will reflect in all those who will pull from the git repository. With no files or configurations added that I could push and reflect to all in the repo. THis means that my exclusions will only work locally and not work on others. Right?. So here I am asking for a way to do programmatically like putting the exclusions in a POM file.

推荐答案

当您使用 Maven 时,您需要在 jacoco 插件中配置排除项,该插件用于捕获覆盖率统计信息并将其传递给声纳.

As you are using Maven, you need to configure your exclusions in the jacoco plugin, which is used to capture the coverage statistics and pass it on to sonar.

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>target/jacoco.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>target/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
</executions>
<configuration>
    <systemPropertyVariables>
        <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
    </systemPropertyVariables>
    <excludes>
        <exclude>snmaddula/app/domain/*.class</exclude>
        <exclude>snmaddula/app/exception/*.class</exclude>
        <exclude>snmaddula/app/filter/*.class</exclude>
        <exclude>snmaddula/app/App.class</exclude>
    </excludes>
</configuration>
</plugin>

参考资料:

这篇关于如何从代码覆盖率中排除某些类?(爪哇)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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