Inicio rápido
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →
Kotest se divide en varios subproyectos independientes que pueden utilizarse por separado:
Puedes decidir apostar por completo a Kotest y usar los tres módulos juntos, o elegir uno o más módulos en combinación con otros proyectos. Por ejemplo, podrías usar la biblioteca de aserciones con JUnit, o el marco de pruebas con otra biblioteca de aserciones como assertj.
Esta página proporciona instrucciones de configuración para varias combinaciones de proyectos y destinos.
Kotest es un proyecto multiplataforma. Si no estás familiarizado, Kotlin se compila para diferentes destinos: JVM, JS, Native, iOS, etc. Si desarrollas para servidor o Android, necesitarás módulos que terminen en JVM, como kotest-property-jvm.
Marco de pruebas
El marco de pruebas de Kotest es compatible con JVM, Javascript y Native. Para habilitar Kotest en múltiples plataformas, combina los pasos para cada plataforma como se detalla en las siguientes pestañas.
- JVM/Gradle
- JVM/Maven
- Kotlin/JS
- Kotlin/Native
- Android
Kotest on the JVM uses the JUnit Platform gradle plugin.
For Gradle 4.6 and higher this is as simple as adding useJUnitPlatform() inside the tasks with type Test
and then adding the Kotest junit5 runner dependency.
If you are using Gradle + Groovy then:
test {
useJUnitPlatform()
}
Or if you are using Gradle + Kotlin then:
tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
And then the dependency:
testImplementation 'io.kotest:kotest-runner-junit5:$version'
A working multiplatform project with JVM, native and Javascript all configured, with unit and data driven test examples, can be found here: https://github.com/kotest/kotest-examples-multiplatform
Add the Kotest multiplatform gradle plugin to your build.
For example:
plugins {
id("io.kotest.multiplatform") version "5.0.2"
}
Add the engine dependency to your commonTest dependencies block:
kotlin {
targets {
js(IR) { // LEGACY or BOTH are unsupported
browser() // to compile for the web
nodejs() // to compile against node
}
}
sourceSets {
val commonTest by getting {
dependencies {
implementation("io.kotest:kotest-framework-engine:$version")
}
}
}
}
Only the new IR compiler backend for Kotlin/JS is supported. If you are compiling JS with the legacy compiler backend then you will not be able to use Kotest for testing.
Write your tests using FunSpec, ShouldSpec or StringSpec.
Tests can be placed in either commonTest or jsTest
source sets. Run your tests using the gradle check command.
The Javascript 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 javascript code.
Tests for Javascript cannot nest tests. This is due to the underlying Javascript test runners (such as Mocha or Karma)
not supporting promises in parent tests, which is incompatible with coroutines and in Kotest every test scope is a coroutine.
This is why the supported specs are limited to FunSpec, ShouldSpec and StringSpec.
The IntelliJ Kotest plugin does not support running common, native or JS tests directly from the IDE using the green run icons. Only execution via gradle is supported.
A working multiplatform project with JVM, native and Javascript all configured, with unit and data driven test examples, can be found here: https://github.com/kotest/kotest-examples-multiplatform
Add the Kotest multiplatform gradle plugin to your build.
For example:
plugins {
id("io.kotest.multiplatform") version "5.0.2"
}
Add the engine dependency to your commonTest dependencies block:
kotlin {
targets {
linuxX64() // can add any supported native targets such as linux, mac, windows etc
}
}
sourceSets {
val commonTest by getting {
dependencies {
implementation("io.kotest:kotest-framework-engine:$version")
}
}
}
Tests can be placed in either commonTest or a specific native sourceset.
Run your tests using the gradle check command.
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.
The IntelliJ Kotest plugin does not support running common, native or JS tests from the IDE. You will need to use
the gradle check task.
For maven you must configure the surefire plugin for junit tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
And then add the Kotest JUnit5 runner to your dependencies section.
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-runner-junit5-jvm</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
Currently, only JVM tests are officially supported in Kotest. Experimental support for instrumented and Robolectric tests is currently under work.
The following steps enable Kotest to be used for unit and integration tests, where the Android framework is not needed or is mocked 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 {
useJUnitPlatform()
}
}
dependencies {
testImplementation 'io.kotest:kotest-runner-junit5:version'
}
To configure the test framework for both JS and JVM, you just combine copy the steps for JVM and JS.
Biblioteca de aserciones
El núcleo de la biblioteca de aserciones es compatible con todos los destinos. Los submódulos funcionan en las plataformas aplicables. Por ejemplo, los matchers de JDBC solo funcionan en JVM ya que JDBC es una biblioteca Java.
- JVM/Gradle
- JVM/Maven
- Multiplatform
Add the following dependency to your build:
testImplementation 'io.kotest:kotest-assertions-core:$version'
Add the following dependency to your build.
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
Add the following dependency to your commonTest dependencies block:
implementation 'io.kotest:kotest-assertions-core:$version'
Alternatively, add the dependency to a specific target. For example, we could add to the Javascript target only.
kotlin {
targets {
js {
browser()
nodejs()
}
}
sourceSets {
val jsTest by getting {
dependencies {
implementation("io.kotest:kotest-assertions-core:$version")
}
}
}
}
Pruebas basadas en propiedades
El framework de pruebas basadas en propiedades está soportado en todas las plataformas.
- JVM/Gradle
- JVM/Maven
- Multiplatform
Add the following dependency to your build:
testImplementation 'io.kotest:kotest-property:$version'
Add the following dependency to your build.
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-property-jvm</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
Add the following dependency to your commonTest dependencies block:
implementation 'io.kotest:kotest-property:$version'
Alternatively, add the dependency to a specific target. For example, we could add to the Javascript target only.
kotlin {
targets {
js {
browser()
nodejs()
}
}
sourceSets {
val jsTest by getting {
dependencies {
implementation("io.kotest:kotest-property:$version")
}
}
}
}
Snapshots
Los snapshots se publican automáticamente en cada commit a master. Si deseas probar la última versión snapshot, configura como se describió anteriormente, cambia la versión a la versión snapshot actual y añade el siguiente repositorio en tu bloque repositories:
https://oss.sonatype.org/content/repositories/snapshots