在使用Kotlin和Jacoco时,是否有一种方法可以排除getter/setter?

3
使用Kotlin和Jacoco一起测试时,有没有一种方法可以排除getter/setter/constructor等方法在测试范围之外?从Jacoco 0.8.7和Kotlin版本1.5或更高版本开始,据说如果使用lateinit,则会被排除在外,但仍会包含在测试报告中。我已经苦苦挣扎了几天,请帮忙解决这个问题。以下是我参考的网站:https://andrey-fomenkov.medium.com/kotlin-jacoco-tuning-compiler-to-skip-generated-code-935fcaeaa391 https://github.com/jacoco/jacoco/wiki/FilteringOptions
1个回答

0

我已经成功地使用了以下方法。

正如您所知,从Jacoco 0.8.2开始,您可以将任何带有@Generated的内容排除在覆盖范围之外。因此我们将使用它。

该过程如下:

  1. 创建CustomAnnotation类
  2. 在Example.kt中的自定义Getter/Setter get()或setter()上方放置@Generated

Qualifier.kt

@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.TYPE, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER)
annotation class Generated

Example.kt

val xxxxx: String
 @Generated
 get() = true
```

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接