Multiplatform testing framework providing unified API for writing tests across all Kotlin platforms with assertions and test annotations.
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-test@2.2.0Kotlin Test is a comprehensive multiplatform testing framework that provides a unified API for writing tests across all Kotlin platforms (JVM, JavaScript, Native, WASM). It offers platform-agnostic test annotations and assertion functions that work consistently across different target platforms, enabling developers to write tests once and run them everywhere.
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test:2.2.0")
}import kotlin.test.*For specific imports:
import kotlin.test.Test
import kotlin.test.BeforeTest
import kotlin.test.AfterTest
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.assertFailsimport kotlin.test.*
class SimpleTest {
@Test
fun testBasicAssertions() {
val expected = 42
val actual = 6 * 7
assertEquals(expected, actual)
assertTrue(actual > 0)
assertNotNull(actual)
}
@Test
fun testExceptionHandling() {
assertFails {
throw IllegalArgumentException("Test exception")
}
val exception = assertFailsWith<IllegalArgumentException> {
throw IllegalArgumentException("Specific exception")
}
assertEquals("Specific exception", exception.message)
}
@BeforeTest
fun setup() {
// Setup code before each test
}
@AfterTest
fun teardown() {
// Cleanup code after each test
}
}Kotlin Test is built around several key components:
Platform-agnostic annotations for marking test functions and lifecycle methods. These annotations are mapped to platform-specific equivalents automatically.
@Target(AnnotationTarget.FUNCTION)
annotation class Test
@Target(AnnotationTarget.FUNCTION)
annotation class BeforeTest
@Target(AnnotationTarget.FUNCTION)
annotation class AfterTest
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
annotation class IgnoreCore assertion functions for verifying boolean conditions, equality, and identity comparisons.
fun assertTrue(actual: Boolean, message: String? = null)
fun assertFalse(actual: Boolean, message: String? = null)
fun <T> assertEquals(expected: T, actual: T, message: String? = null)
fun <T> assertNotEquals(illegal: T, actual: T, message: String? = null)
fun <T> assertSame(expected: T, actual: T, message: String? = null)
fun <T> assertNotSame(illegal: T, actual: T, message: String? = null)Assertions for type checking and null safety validation.
inline fun <reified T> assertIs(value: Any?, message: String? = null): T
inline fun <reified T> assertIsNot(value: Any?, message: String? = null)
fun <T : Any> assertNotNull(actual: T?, message: String? = null): T
fun assertNull(actual: Any?, message: String? = null)Comprehensive assertions for working with collections, arrays, and content comparison.
fun <T> assertContains(iterable: Iterable<T>, element: T, message: String? = null)
fun <T> assertContains(array: Array<T>, element: T, message: String? = null)
fun <K, V> assertContains(map: Map<K, V>, key: K, message: String? = null)
fun assertContains(charSequence: CharSequence, char: Char, ignoreCase: Boolean = false, message: String? = null)
fun <T> assertContentEquals(expected: Iterable<T>?, actual: Iterable<T>?, message: String? = null)
fun <T> assertContentEquals(expected: Array<T>?, actual: Array<T>?, message: String? = null)Collection and Content Assertions
Assertions for testing exception handling and failure scenarios.
fun fail(message: String? = null): Nothing
fun fail(message: String? = null, cause: Throwable? = null): Nothing
inline fun assertFails(block: () -> Unit): Throwable
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, block: () -> Unit): T
inline fun <T> expect(expected: T, block: () -> T)
fun todo(block: () -> Unit)The core assertion interface that can be customized for different testing frameworks and platforms.
interface Asserter {
fun fail(message: String?): Nothing
fun fail(message: String?, cause: Throwable?): Nothing
fun assertTrue(message: String?, actual: Boolean): Unit
fun assertEquals(message: String?, expected: Any?, actual: Any?): Unit
fun assertNotEquals(message: String?, illegal: Any?, actual: Any?): Unit
fun assertSame(message: String?, expected: Any?, actual: Any?): Unit
fun assertNotSame(message: String?, illegal: Any?, actual: Any?): Unit
fun assertNull(message: String?, actual: Any?): Unit
fun assertNotNull(message: String?, actual: Any?): Unit
}
val asserter: Asserterinterface AsserterContributor {
fun contribute(): Asserter?
}
object DefaultAsserter : Asserter