How can I exclude all Jetpack Compose's Preview functions from Jacoco report?
Asked Answered
H

1

6

When we run Jacoco code coverage of Jetpack Compose function, I like to exclude all Preview functions.

I can do so using

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
internal annotation class ExcludeFromJacocoGeneratedReport

Then for the function I like to exclude from the report, I annotate it with

@ExcludeFromJacocoGeneratedReport
@Preview(
    name = "Name"
)
@Composable
private fun MyComposePreview() {
    // ... function content
}

This works. But I have to add the annotation to all my Preview functions. I am hoping I can do it in the build.gradle file to exclude them all.

Is there a way to do so?

Heimer answered 7/12, 2022 at 9:57 Comment(1)
I'm using compose-bom v2024.05.00 and jacoco v0.8.12 and this approach doesn't work. Were you able to keep it working after updating the libraries?Rocky
U
1

The documentation for Preview says that it can be applied to:

Annotation classes, that could then be used to annotate @Composable methods or other annotation classes, which will then be considered as indirectly annotated with that Preview.

So I suppose you could create an annotation "PreviewExludedFromJacoco" that you annotate with Preview. This is not exactly what you ask for, but it would allow you to have only one annotation for each Preview function.

Unlock answered 27/1, 2023 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.