Links
Comment on page

Isolate Konsist Tests

Aim for better test separation.
The konsist library can be added to the project by adding the dependency on the existing test source set (see Broken link).
test sorce directory
As the project grows it may be desirable to isolate tests further e.g. separate unit tests from Konsist tests.
To organize tests add a new test directory, module, or project. See preconfigured Starter Projects.

Dedicated Source Set (Spring Projects and Pure Kotlin Projects)

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.
Gradle (Kotlin)
Gradle (Groovy)
Maven
Use the Gradle built-in JVM Test Suite Plugin to define the konsistTest source set. Add a testing block to the project configuration:
// app/build.gradle.kts
plugins {
`jvm-test-suite`
}
testing {
suites {
register("konsistTest", JvmTestSuite::class) {
dependencies {
// Add 'main' source set dependency
implementation(project())
// Add Konsist dependency
implementation("com.lemonappdev:konsist:0.13.0")
}
}
}
}
// Optional block to run Konsist tests together with the Gradle 'check' task
tasks.named("check") {
dependsOn(testing.suites.named("konsistTest"))
}
Use the Gradle built-in JVM Test Suite Plugin to define the konsistTest source set. Add a testing block to the project configuration:
// app/build.gradle
plugins {
id 'jvm-test-suite'
}
testing {
suites {
test {
useJUnitJupiter()
}
konsistTest(JvmTestSuite) {
dependencies {
// Add 'main' source set dependency
implementation project()
// Add Konsist dependency
implementation "com.lemonappdev:konsist:0.13.0"
}
targets {
all {
testTask.configure {
shouldRunAfter(test)
}
}
}
}
}
}
// Optional block to run Konsist tests together with the Gradle 'check' task
tasks.named('check') {
dependsOn(testing.suites.konsistTest)
}
Use the Maven Build Helper Plugin to define the konsistTest test source directory. Add plugin config to the project configuration:
# app/pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-konsist-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/konsistTest/kotlin</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Create app/src/konsistTest/kotlin folder and reload the project. The IDE will present a new konsistTest source set in the app module.
konsistTest sorce directory
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:
Gradle
Maven
./gradlew app:konsistTest
mvn test

Dedicated Module (Android Projects and Kotlin Multiplatform Projects)

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.

Add konsistTest Module:

Gradle (Kotlin)
Gradle (Groovy)
Create konsistTest/src/test/kotlin directory in the project root:
Add module include inside settings.gradle.kts file:
// settings.gradle.kts
include(":konsistTest")
Create konsistTest/src/test/kotlin directory in the project root:
Add module include inside settings.gradle.kts file:
// settings.gradle
include ':konsistTest'
For Android projects add com.android.library plugin in the konsistTest/scr/test/kotlin/build.gradle file.
Refresh/Sync the Gradle Project in IDE.

Add konsistTest Module Build Script

Gradle Kotlin
Gradle Groovy
Add konsistTest/build.gradle.kts file.
Add konsistTest/build.gradle file.
The content of the file will differ depending on the project type (Android, Spring, KMP) and the selected testing framework (JUnit4, JUnit5, KoTest).
Copy content from the file from one of the starter-projects e.g. copy from starter-projects/konsist-starter-spring-gradle-groovy-kotest/build.gradle.kts
Refresh/Sync the Gradle Project in IDE.

Running Konsist Tests

To execute tests in konsistTest module run:
./gradlew konsistTest:test --rerun-tasks
The --rerun-tasks Gradle flag is required when Konsist tests are placed in a distinct module. When the module is unchanged Gradle assumes the tests are up-to-date, so these tests are skipped. This can lead to misleading test outcomes, as Gradle isn't aware that these tests are actually evaluating code in other modules.
To execute all unit tests besides tests in the konsistTest module run:
./gradlew test -x konsistTest:test