Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests with coverage docs with Gradle examples #21896

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions docs/src/main/asciidoc/tests-with-coverage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ Now we need to add Jacoco to our project. To do this we need to add the followin
</dependency>
----

If you are using Gradle, add this to your `build.gradle`:

[source,gradle]
----
testImplementation 'io.quarkus:quarkus-jacoco'
----

This Quarkus extension takes care of everything that would usually be done via the Jacoco maven plugin, so no additional
config is required.

Expand Down Expand Up @@ -239,6 +246,27 @@ In addition to including the `quarkus-jacoco` extension in your pom you will nee
WARNING: This config will only work if at least one `@QuarkusTest` is being run. If you are not using `@QuarkusTest` then
you can simply use the Jacoco plugin in the standard manner with no additional config.

If you are using Gradle, add this to your `build.gradle`:

[source,gradle,subs=attributes+]
----
plugins {
id 'jacoco' <1>
}

test {
finalizedBy jacocoTestReport
jacoco {
excludeClassLoaders = ["*QuarkusClassLoader"] <2>
destinationFile = layout.buildDirectory.file("jacoco-quarkus.exec").get().asFile <2>
}
jacocoTestReport.enabled = false <3>
}
----
<1> Add the `jacoco` gradle plugin
<2> This config tells it to ignore `@QuarkusTest` related classes, as they are loaded by `QuarkusClassLoader`
<3> Set this config to `false` if you are also using the `quarkus-jacoco` extension and have at least one `@QuarkusTest`. The default `jacocoTestReport` task can be skipped since `quarkus-jacoco` will generate the combined report of regular unit tests and `@QuarkusTest` classes since the execution data is recorded in the same file.

=== Coverage for Integration Tests

To get code coverage data from integration tests, the following need to be requirements need to be met:
Expand Down Expand Up @@ -375,6 +403,49 @@ You can set thresholds for code coverage using the Jacoco Maven plugin. Note the
</build>
----

If you are using Gradle, add this to your `build.gradle`:

[source, gradle]
----
jacocoTestCoverageVerification {
executionData.setFrom("$project.buildDir/jacoco-quarkus.exec")
violationRules {
rule {
limit {
counter = 'INSTRUCTION'
value = 'COVEREDRATIO'
minimum = 0.80
}
limit {
counter = 'BRANCH'
value = 'COVEREDRATIO'
minimum = 0.72
}
}
}
}
check.dependsOn jacocoTestCoverageVerification
----

Excluding classes from the verification task can be configured as following:

[source,gradle]
----
jacocoTestCoverageVerification {
afterEvaluate { <1>
classDirectories.setFrom(files(classDirectories.files.collect { <2>
fileTree(dir: it, exclude: [
"org/example/package/**/*" <3>
])
}))
}
}
----
<1> `classDirectories` needs to be read after evaluation phase in Gradle
<2> Currently, there is a bug in Gradle JaCoCo which requires the `excludes` to be specified in this manner - https://github.com/gradle/gradle/issues/14760. Once this issue is fixed, excludes
<3> Exclude all classes in `org/example/package` package


== Conclusion

You now have all the information you need to study the coverage of your tests!
Expand Down