Configuración
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →
El entorno de pruebas Kotest es compatible con todas las plataformas, incluyendo JVM, JavaScript, Native y Wasm. Para habilitar Kotest en múltiples plataformas, sigue los pasos correspondientes a la plataforma que estés utilizando, tal como se detalla en las siguientes pestañas.
La compatibilidad con KMP en Kotest 6.0 ha cambiado respecto a versiones anteriores. Ya no se utiliza un plugin de compilación, sino una configuración simplificada. Consulta el resto de esta página para ver cómo configurar Kotest para KMP en Kotest 6.0 y versiones posteriores.
Al ejecutar la tarea de pruebas de Gradle, Gradle almacenará en caché la salida e informará que no se han ejecutado pruebas si no ha cambiado ningún código fuente. Consulta la sección sobre cómo volver a ejecutar pruebas para obtener detalles sobre cómo deshabilitar este comportamiento.
Kotest ofrece un plugin para IntelliJ que mejora la experiencia de usuario, incluyendo la capacidad de ejecutar pruebas individuales, ventanas de herramientas para mostrar diseños de pruebas y saltar al código fuente.
- JVM
- Kotlin/JS
- Kotlin/WasmJS
- Kotlin/Native
- Android
- Multiplatform
A working project with JVM support can be found here: https://github.com/kotest/kotest-examples
Kotest on the JVM builds atop of the JUnit Platform project which is widely supported in the JVM ecosystem.
To use the JUnit Platform support, first configure Gradle to use JUnit platform support:
tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
Andd then add the following dependency to your build:
dependencies {
testImplementation("io.kotest:kotest-runner-junit5:<kotest-version>")
}
And then execute the test task in gradle, or run tests directly from the IDE.
For enhanced support for jump-to-source and re-running failed tests from the test results tree, add the Kotest Gradle plugin to to your build.
For example:
plugins {
id("io.kotest").version("<kotest-version>")
}
A working JS project can be found here: https://github.com/kotest/kotest-examples
Add the Kotest gradle plugin and Google KSP plugin to to your build. Note, place the KSP plugin before the Kotest plugin in the plugins block. This restriction will be fixed in a future release.
For example:
plugins {
id("com.google.devtools.ksp").version("<ksp-version>")
id("io.kotest").version("<kotest-version>")
}
Add the kotest-framework-engine dependency to your commonTest or jsTest source set:
kotlin {
js()
sourceSets {
commonTest {
dependencies {
implementation("io.kotest:kotest-framework-engine:<kotest-version>")
}
}
}
}
Tests can be placed in either commonTest or jsTest.
Run your tests using the jsTest gradle task.
The JS test engine is feature limited when compared to the JVM test engine. The major restriction is that annotation based configuration will not work as Kotlin does not expose annotations at runtime to JS code.
A working WasmJS project can be found here: https://github.com/kotest/kotest-examples
Add the Kotest gradle plugin and Google KSP plugin to to your build.
For example:
plugins {
id("io.kotest").version("<kotest-version>")
id("com.google.devtools.ksp").version("<ksp-version>")
}
Add the kotest-framework-engine dependency to your commonTest or wasmJsTest source set:
kotlin {
wasmJs()
sourceSets {
commonTest {
dependencies {
implementation("io.kotest:kotest-framework-engine:<kotest-version>")
}
}
}
}
Tests can be placed in either commonTest or wasmJsTest.
Run your tests using the wasmJsTest gradle task.
The WasmJS test engine is feature limited when compared to the JVM test engine. The major restriction is that annotation based configuration will not work as Kotlin does not expose annotations at runtime to Wasm code.
A working native project with Linux, Windows and MacOS targets configured, with unit and data driven test examples, can be found here: https://github.com/kotest/kotest-examples
Add the Kotest gradle plugin and Google KSP plugin to to your build.
For example:
plugins {
id("io.kotest").version("<kotest-version>")
id("com.google.devtools.ksp").version("<ksp-version>")
}
Add the kotest-framework-engine dependency to your commonTest, nativeTest or platform specific sourceset:
kotlin {
linuxX64() // add any supported native target
sourceSets {
commonTest {
dependencies {
implementation("io.kotest:kotest-framework-engine:<kotest-version>")
}
}
}
}
Tests can be placed in either commonTest or a specific native sourceset.
Run your tests using the standard test tasks, for example linuxX86Test.
The native test engine is feature limited when compared to the JVM test engine. The major restriction is that annotation based configuration will not work as Kotlin does not expose annotations at runtime to native code.
Currently, only Unit tests are supported in Kotest.
The following steps enable Kotest to be used for unit tests - where the Android framework is not needed or is mocked - and that usually reside in the
src/test folder of your module.
Kotest on Android uses the JUnit Platform gradle plugin. This requires configuring the android test options block in your build file and then adding the Kotest junit5 runner dependency.
android.testOptions {
unitTests.all {
it.useJUnitPlatform()
}
}
dependencies {
testImplementation 'io.kotest:kotest-runner-junit5:version'
}
A working Android project with unit and data driven test examples, can be found here: https://github.com/kotest/kotest-examples
A working multiplatform project with JVM, JS and native targets, and unit and data driven test examples, can be found here: https://github.com/kotest/kotest-examples
Add the Kotest gradle plugin and Google KSP plugin to to your build.
For example:
plugins {
id("io.kotest") version "<kotest-version>"
id("com.google.devtools.ksp") version "<kotlin-verson>-<ksp-version>"
}
Add the kotest-framework-engine dependency to your commonTest source set:
kotlin {
sourceSets {
commonTest {
dependencies {
implementation("io.kotest:kotest-framework-engine:<kotest-version>")
}
}
}
}
Tests can be placed in either commonTest or a platform specific directory such as jsTest or macosX64Test etc.
Run your tests using the gradle check task, or a platform specific test task such as macosX64Test
The JS, Wasm and native test engines are feature limited when compared to the JVM test engine. The major restriction is that annotation based configuration will not work as Kotlin does not expose annotations at runtime to non-JVM platforms.
Volver a ejecutar pruebas
Por defecto, la compilación incremental de Gradle omitirá la ejecución de pruebas si no ha cambiado ningún código fuente, marcando la tarea como UP-TO-DATE. Esto puede resultar inconveniente durante la depuración.
Para forzar que tus pruebas se ejecuten cada vez, puedes añadir temporalmente la siguiente configuración en tu archivo build.gradle.kts:
tasks.withType<Test>().configureEach {
logger.lifecycle("UP-TO-DATE check for $name is disabled, forcing it to run.")
outputs.upToDateWhen { false }
}
Alternativa rápida: Para volver a ejecutar una sola vez sin modificar los archivos de construcción, puedes usar la opción --rerun desde la línea de comandos:
./gradlew test --rerun