我将如何添加注释以从 jacoco 代码覆盖率报告中排除方法? [英] How would I add an annotation to exclude a method from a jacoco code coverage report?
问题描述
我有一些 Java 代码要从代码覆盖率中排除.我该怎么做?我希望能够添加注释.有没有办法配置或扩展 jacoco(在 gradle 中使用)来使用它?
I have some code in Java that I want to exclude from code coverage. How would I do this? I want to be able to add an annotation. Is there a way to configure or extend jacoco (as used in gradle) to use this?
示例:
public class Something
{
@ExcludeFromCodeCoverage
public void someMethod() {}
}
推荐答案
由于没有直接答案,所以做了一些研究并发现了这个 PR.
Since there are no direct answers to this, did a bit of research and came across this PR.
https://github.com/jacoco/jacoco/pull/822/files
private static boolean matches(final String annotation) {
final String name = annotation
.substring(Math.max(annotation.lastIndexOf('/'),
annotation.lastIndexOf('$')) + 1);
return name.contains("Generated")
}
您可以创建名称包含Generated"的任何注释.我在我的代码库中创建了以下内容,以排除包含在 Jacoco 报告中的方法.
You can create any annotation with name containing "Generated". I've created the following in my codebase to exclude methods from being included in Jacoco report.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExcludeFromJacocoGeneratedReport {}
在您的方法中使用此注释可将其免于覆盖,如下所示.
Use this annotation in your methods to exempt it from coverage as below.
public class Something
{
@ExcludeFromJacocoGeneratedReport
public void someMethod() {}
}
这篇关于我将如何添加注释以从 jacoco 代码覆盖率报告中排除方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!