Isolate Konsist Tests
Aim for better test separation.
Last updated
Aim for better test separation.
Last updated
Typically, it's advisable to consolidate all Konsist tests in a unified location. This approach is preferred because these tests are often designed to validate the structure of the entire project's codebase. There are three potential options for storing Konsist tests in project codebase:
Android | Spring | KMP | Pure Kotlin | |
---|---|---|---|---|
â | â | â | â | |
â | â | â | â | |
â | â | â | â |
Recommended approach is to use Dedicated konsistTest Source Set or a #dedicated-module. These approaches allows to easily isolate Konsist tests from other types of tests e.g. separate unit tests
from Konsist tests
.
The Konsist library can be added to the project by adding the dependency on the existing test
source set .
To execute tests run ./gradlew test
command.
The downside of this approach is that various types of tests are mixed in test
source set e.g. unit tests
and Konsist tests
.
This section demonstrates how to add the konsistTest
test source directory inside the app
module. This configuration is mostly useful for Spring and Kotlin projects.
This page describes the test located in the app
module with the build config file located in app
a folder. If the project does not contain any module then configuration should be applied in the root build config file.
This test directory will have a kotlin
folder containing Kotlin code.
Use the Gradle built-in JVM Test Suite Plugin to define the konsistTest
source set. Add a testing
block to the project configuration:
Create app/src/konsistTest/kotlin
folder and reload the project. The IDE will present a new konsistTest
source set in the app
module.
The konsistTest
test source folder works exactly like the build-in test
source folder, so Kosist tests can be defined and executed in a similar way:
This section demonstrates how to add the konsistTest
module to the project. This configuration is primarily helpful for Android projects and Kotlin Multiplatform (KMP) projects, however, this approach will also work with Spring and pure Kotlin projects.
The Android Gradle Plugin is used to build Android apps. The Android Gradle Plugin is not compatible with the JVM Test Suite Plugin and it does not allow adding new source sets. To fully isolate tests a new module is required.
The Kotlin Multiplatform project contains modules with code for different platforms. To decouple Konsist tests from a single platform dedicated module containing Konsist test should be added.
konsistTest
Module:Create konsistTest/src/test/kotlin
directory in the project root:
Add module include inside settings.gradle.kts
file:
Gradle's default behavior assumes that a module's code is up-to-date if the module itself hasn't been modified. This can lead to issues when Konsist tests are placed in a separate module. In such cases, Gradle may skip these tests, believing they're unnecessary.
However, this approach doesn't align well with Konsist's functionality. Konsist analyzes the entire codebase, not just individual modules. As a result, when Gradle skips Konsist tests based on its module-level change detection, it fails to account for potential changes in other modules that Konsist would typically examine.
There are few solutions to this problem.
An alternative solution for this problem is to define konsistTest
module as always being out of date:
To execute all unit tests besides tests in the konsistTest
module run:
./gradlew test -x konsistTest:test
To avoid manually passing --rerun-tasks
flag each time a custom konsistCheck
task can be added to the root build config file:
Add to root build.gradle.kts
:
After adding konsistCheck
task run ./gradlew konsistCheck
to execute all Konsist tests.