From b8629c1b6fad44853828f7e2b87425193ce12332 Mon Sep 17 00:00:00 2001 From: Brian Wyka <46069296+brianwyka@users.noreply.github.com> Date: Thu, 2 Dec 2021 16:09:59 -0500 Subject: [PATCH] Update tests with coverage docs with Gradle examples --- .../main/asciidoc/tests-with-coverage.adoc | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/docs/src/main/asciidoc/tests-with-coverage.adoc b/docs/src/main/asciidoc/tests-with-coverage.adoc index 83407a5fd500d..780711dfeb458 100644 --- a/docs/src/main/asciidoc/tests-with-coverage.adoc +++ b/docs/src/main/asciidoc/tests-with-coverage.adoc @@ -173,6 +173,13 @@ Now we need to add Jacoco to our project. To do this we need to add the followin ---- +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. @@ -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: @@ -375,6 +403,49 @@ You can set thresholds for code coverage using the Jacoco Maven plugin. Note the ---- +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!