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 configuración de Kotlin Multiplatform en Kotest 6.0 ha cambiado respecto a versiones anteriores. El plugin de Gradle de Kotest ha sido
renombrado a io.kotest. Además, el plugin del compilador ha sido reemplazado por un método más robusto que utiliza KSP.
Consulta el resto de esta página para obtener detalles sobre 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()
}
And 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.
A working Android project with unit, instrumented and data driven test examples, can be found here: https://github.com/kotest/kotest-examples
Android supports two types of tests:
- Unit tests, which are run on the local machine and are also referred to as "host" tests
- Instrumented tests, which are run on an Android device or emulator and are also referred to as "device" tests
For unit tests, Kotest 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")
}
For instrumented tests, Kotest uses the JUnit4 Runner.
This requires adding the Kotest JUnit4 runner dependency to your build using the androidTestImplementation configuration.
dependencies {
androidTestImplementation("io.kotest:kotest-runner-junit4:version")
}
When writing an instrumented test, you should use the androidTest source set to place your test code, and you must
annotate your spec with @RunWith(KotestTestRunner::class). For example:
@RunWith(KotestTestRunner::class)
class ComposeWithKotest : FreeSpec() {
@get:Rule
val composeTestRule: ComposeContentTestRule = createComposeRule()
init {
"should have initial state of 0" {
composeTestRule.setContent {
TestComposable()
}
composeTestRule.onNodeWithText("0").assertExists()
composeTestRule.onNodeWithText("Click me!").assertExists()
}
"should increment when clicked" {
composeTestRule.setContent {
TestComposable()
}
composeTestRule.onNodeWithText("0").assertExists()
composeTestRule.onNodeWithText("Click me!").assertExists()
}
}
}
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