Java 如何添加注释以从 jacoco 代码覆盖率报告中排除方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47824761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would I add an annotation to exclude a method from a jacoco code coverage report?
提问by Don Rhummy
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?
我有一些 Java 代码要从代码覆盖率中排除。我该怎么做?我希望能够添加注释。有没有办法配置或扩展 jacoco(在 gradle 中使用)来使用它?
Example:
例子:
public class Something
{
@ExcludeFromCodeCoverage
public void someMethod() {}
}
回答by Godin
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?
我有一些 Java 代码要从代码覆盖率中排除。我该怎么做?我希望能够添加注释。有没有办法配置或扩展 jacoco(在 gradle 中使用)来使用它?
As of today there is no such feature in latest released version of JaCoCo (0.7.9
). Only whole classes can be excluded.
截至今天,最新发布的 JaCoCo ( 0.7.9
)版本中没有此类功能。只能排除整个类。
On page https://github.com/jacoco/jacoco/wiki/FilteringOptions#annotation-based-filtering(which is dedicated for developers) this is recorded as an idea for future versions.
在页面https://github.com/jacoco/jacoco/wiki/FilteringOptions#annotation-based-filtering(专供开发人员使用)这被记录为未来版本的想法。
Official JaCoCo documentation contains information about how to obtain latest unreleased build as well as list of unreleased changesfor next version - http://www.jacoco.org/jacoco/trunk/doc/changes.html, which includes various filters, among which filtering of methods that are generated by Lombok and Groovy and marked by annotations lombok.Generated
and groovy.transform.Generated
respectively. Potentially you can abuse this, but I wouldn't recommend to do so for many various reasons.
官方 JaCoCo 文档包含有关如何获取最新未发布版本以及下一版本未发布更改列表的信息- http://www.jacoco.org/jacoco/trunk/doc/changes.html,其中包括各种过滤器,其中过滤由 Lombok 和 Groovy 生成lombok.Generated
并groovy.transform.Generated
分别由注释和标记的方法。您可能会滥用此功能,但出于多种原因,我不建议您这样做。
回答by Markus Schulte
Tl;dr
Tl;博士
Use annotation @lombok.Generated
from Lombok.
使用@lombok.Generated
来自Lombok 的注释。
Explanation
解释
Jacoco integrates with Lombok. Code generated by Lombok is excluded from Jacoco coverage by default (see Release 0.8.0 in Jacoco changelog). You can misuse lombok.Generated
at your method for it being excluded from the coverage report.
Jacoco 与Lombok集成。Lombok 生成的代码默认被排除在 Jacoco 覆盖范围之外(参见Jacoco 更新日志中的 0.8.0 版)。您可以滥用lombok.Generated
您的方法,因为它被排除在覆盖率报告之外。
回答by Ajinkya
The new feature has been added in the 0.8.2 release of JaCoCo which filters out the classes and methods annotated with @Generated. For details please see the documentation below:
JaCoCo 0.8.2 版本中添加了新功能,它过滤掉了用@Generated 注释的类和方法。有关详细信息,请参阅以下文档:
Classes and methods annotated with annotation whose retention policy is runtime or class and whose simple name is Generated are filtered out during generation of report (GitHub #731).
保留策略为运行时或类且简单名称为 Generated 的注解的类和方法在报告生成期间被过滤掉 (GitHub #731)。