Package kotlin.test

Annotations to mark test functions and top-level functions for performing assertions in tests.

Types

Common
JVM
JS
Native
1.0

Asserter

Abstracts the logic for performing assertions. Specific implementations of Asserter can use JUnit or TestNG assertion facilities.

interface Asserter
Common
JVM
JS
Native
1.0

AsserterContributor

Checks applicability and provides Asserter instance

interface AsserterContributor
JS
1.1

AssertionResult

Describes the result of an assertion execution.

interface AssertionResult
Common
JVM
JS
Native
1.0

DefaultAsserter

Default Asserter implementation to avoid dependency on JUnit or TestNG.

object DefaultAsserter : Asserter
JS
1.1

FrameworkAdapter

Serves as a bridge to a testing framework.

interface FrameworkAdapter

Annotations

Native
1.3

AfterClass

Marks a function to be executed after a suite. Not supported in Kotlin/Common.

annotation class AfterClass

AfterTest

Marks a function to be invoked after each test.

Common
JS
Native
1.0
annotation class AfterTest
JUnit
1.0
typealias AfterTest = After
JUnit5
1.0
typealias AfterTest = AfterEach
TestNG
1.0
typealias AfterTest = AfterMethod
Native
1.3

BeforeClass

Marks a function to be executed before a suite. Not supported in Kotlin/Common.

annotation class BeforeClass

BeforeTest

Marks a function to be invoked before each test.

Common
JS
Native
1.0
annotation class BeforeTest
JUnit
1.0
typealias BeforeTest = Before
JUnit5
1.0
typealias BeforeTest = BeforeEach
TestNG
1.0
typealias BeforeTest = BeforeMethod

Ignore

Marks a test or a suite as ignored.

Common
JS
Native
1.0
annotation class Ignore
JUnit
1.0
typealias Ignore = Ignore
JUnit5
1.0
typealias Ignore = Disabled
TestNG
1.0
typealias Ignore = Ignore

Test

Marks a function as a test.

Common
JS
Native
1.0
annotation class Test
JUnit
1.0
typealias Test = Test
JUnit5
1.0
typealias Test = Test
TestNG
1.0
typealias Test = Test

Properties

Common
JVM
JS
Native
1.0

asserter

Current adapter providing assertion implementations

val asserter: Asserter

Functions

Common
JVM
JS
Native
1.5

assertContains

Asserts that the iterable contains the specified element, with an optional message.

fun <T> assertContains(
    iterable: Iterable<T>,
    element: T,
    message: String? = null
): Unit

Asserts that the sequence contains the specified element, with an optional message.

fun <T> assertContains(
    sequence: Sequence<T>,
    element: T,
    message: String? = null
): Unit

Asserts that the array contains the specified element, with an optional message.

fun <T> assertContains(
    array: Array<T>,
    element: T,
    message: String? = null
): Unit
fun assertContains(
    array: ByteArray,
    element: Byte,
    message: String? = null
): Unit
fun assertContains(
    array: ShortArray,
    element: Short,
    message: String? = null
): Unit
fun assertContains(
    array: IntArray,
    element: Int,
    message: String? = null
): Unit
fun assertContains(
    array: LongArray,
    element: Long,
    message: String? = null
): Unit
fun assertContains(
    array: BooleanArray,
    element: Boolean,
    message: String? = null
): Unit
fun assertContains(
    array: CharArray,
    element: Char,
    message: String? = null
): Unit
fun assertContains(
    array: UByteArray,
    element: UByte,
    message: String? = null
): Unit
fun assertContains(
    array: UShortArray,
    element: UShort,
    message: String? = null
): Unit
fun assertContains(
    array: UIntArray,
    element: UInt,
    message: String? = null
): Unit
fun assertContains(
    array: ULongArray,
    element: ULong,
    message: String? = null
): Unit

Asserts that the range contains the specified value, with an optional message.

fun assertContains(
    range: IntRange,
    value: Int,
    message: String? = null
): Unit
fun assertContains(
    range: LongRange,
    value: Long,
    message: String? = null
): Unit
fun <T : Comparable<T>> assertContains(
    range: ClosedRange<T>,
    value: T,
    message: String? = null
): Unit
fun assertContains(
    range: CharRange,
    value: Char,
    message: String? = null
): Unit
fun assertContains(
    range: UIntRange,
    value: UInt,
    message: String? = null
): Unit
fun assertContains(
    range: ULongRange,
    value: ULong,
    message: String? = null
): Unit

Asserts that the map contains the specified key, with an optional message.

fun <K, V> assertContains(
    map: Map<K, V>,
    key: K,
    message: String? = null
): Unit

Asserts that the charSequence contains the specified char, with an optional message.

fun assertContains(
    charSequence: CharSequence,
    char: Char,
    ignoreCase: Boolean = false,
    message: String? = null
): Unit

Asserts that the charSequence contains the specified other char sequence as a substring, with an optional message.

fun assertContains(
    charSequence: CharSequence,
    other: CharSequence,
    ignoreCase: Boolean = false,
    message: String? = null
): Unit

Asserts that the charSequence contains at least one match of the specified regular expression regex, with an optional message.

fun assertContains(
    charSequence: CharSequence,
    regex: Regex,
    message: String? = null
): Unit
Common
JVM
JS
Native
1.5

assertContentEquals

Asserts that the expected iterable is structurally equal to the actual iterable, i.e. contains the same number of the same elements in the same order, with an optional message.

fun <T> assertContentEquals(
    expected: Iterable<T>?,
    actual: Iterable<T>?,
    message: String? = null
): Unit
fun <T> assertContentEquals(
    expected: Set<T>?,
    actual: Set<T>?,
    message: String? = null
): Unit

Asserts that the expected sequence is structurally equal to the actual sequence, i.e. contains the same number of the same elements in the same order, with an optional message.

fun <T> assertContentEquals(
    expected: Sequence<T>?,
    actual: Sequence<T>?,
    message: String? = null
): Unit

Asserts that the expected array is structurally equal to the actual array, i.e. contains the same number of the same elements in the same order, with an optional message.

fun <T> assertContentEquals(
    expected: Array<T>?,
    actual: Array<T>?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: ByteArray?,
    actual: ByteArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: ShortArray?,
    actual: ShortArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: IntArray?,
    actual: IntArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: LongArray?,
    actual: LongArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: FloatArray?,
    actual: FloatArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: DoubleArray?,
    actual: DoubleArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: BooleanArray?,
    actual: BooleanArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: CharArray?,
    actual: CharArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: UByteArray?,
    actual: UByteArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: UShortArray?,
    actual: UShortArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: UIntArray?,
    actual: UIntArray?,
    message: String? = null
): Unit
fun assertContentEquals(
    expected: ULongArray?,
    actual: ULongArray?,
    message: String? = null
): Unit
Common
JVM
JS
Native
1.0

assertEquals

Asserts that the expected value is equal to the actual value, with an optional message.

fun <T> assertEquals(
    expected: T,
    actual: T,
    message: String? = null
): Unit

Asserts that the difference between the actual and the expected is within an absoluteTolerance, with an optional message.

fun assertEquals(
    expected: Double,
    actual: Double,
    absoluteTolerance: Double,
    message: String? = null
): Unit
fun assertEquals(
    expected: Float,
    actual: Float,
    absoluteTolerance: Float,
    message: String? = null
): Unit
Common
JVM
JS
Native
1.0

assertFails

Asserts that given function block fails by throwing an exception.

fun assertFails(block: () -> Unit): Throwable
fun assertFails(
    message: String?,
    block: () -> Unit
): Throwable
Common
JVM
JS
Native
1.0

assertFailsWith

Asserts that a block fails with a specific exception of type T being thrown.

fun <T : Throwable> assertFailsWith(
    message: String? = null,
    block: () -> Unit
): T

Asserts that a block fails with a specific exception of type exceptionClass being thrown.

fun <T : Throwable> assertFailsWith(
    exceptionClass: KClass<T>,
    block: () -> Unit
): T
fun <T : Throwable> assertFailsWith(
    exceptionClass: KClass<T>,
    message: String?,
    block: () -> Unit
): T
Common
JVM
JS
Native
1.0

assertFalse

Asserts that the given block returns false.

fun assertFalse(
    message: String? = null,
    block: () -> Boolean
): Unit

Asserts that the expression is false with an optional message.

fun assertFalse(
    actual: Boolean,
    message: String? = null
): Unit
JVM
1.0

assertFalseNoInline

fun assertFalseNoInline(
    message: String? = null,
    block: () -> Boolean
): Unit
Common
JVM
JS
Native
1.5

assertIs

Asserts that value is of type T, with an optional message.

fun <T> assertIs(value: Any?, message: String? = null): T
Common
JVM
JS
Native
1.5

assertIsNot

Asserts that value is not of type T, with an optional message.

fun <T> assertIsNot(
    value: Any?,
    message: String? = null
): Unit
Common
JVM
JS
Native
1.0

assertNotEquals

Asserts that the actual value is not equal to the illegal value, with an optional message.

fun <T> assertNotEquals(
    illegal: T,
    actual: T,
    message: String? = null
): Unit

Asserts that the difference between the actual and the illegal is not within an absoluteTolerance, with an optional message.

fun assertNotEquals(
    illegal: Double,
    actual: Double,
    absoluteTolerance: Double,
    message: String? = null
): Unit
fun assertNotEquals(
    illegal: Float,
    actual: Float,
    absoluteTolerance: Float,
    message: String? = null
): Unit
Common
JVM
JS
Native
1.0

assertNotNull

Asserts that the actual value is not null, with an optional message.

fun <T : Any> assertNotNull(
    actual: T?,
    message: String? = null
): T

Asserts that the actual value is not null, with an optional message and a function block to process the not-null value.

fun <T : Any, R> assertNotNull(
    actual: T?,
    message: String? = null,
    block: (T) -> R
): Unit
JVM
1.0

assertNotNullNoInline

fun <T : Any, R> assertNotNullNoInline(
    actual: T?,
    message: String? = null,
    block: (T) -> R
): Unit
Common
JVM
JS
Native
1.0

assertNotSame

Asserts that actual is not the same instance as illegal, with an optional message.

fun <T> assertNotSame(
    illegal: T,
    actual: T,
    message: String? = null
): Unit
Common
JVM
JS
Native
1.0

assertNull

Asserts that the actual value is null, with an optional message.

fun assertNull(actual: Any?, message: String? = null): Unit
Common
JVM
JS
Native
1.0

assertSame

Asserts that expected is the same instance as actual, with an optional message.

fun <T> assertSame(
    expected: T,
    actual: T,
    message: String? = null
): Unit
Common
JVM
JS
Native
1.0

assertTrue

Asserts that the given block returns true.

fun assertTrue(
    message: String? = null,
    block: () -> Boolean
): Unit

Asserts that the expression is true with an optional message.

fun assertTrue(
    actual: Boolean,
    message: String? = null
): Unit
JVM
1.0

assertTrueNoInline

fun assertTrueNoInline(
    message: String? = null,
    block: () -> Boolean
): Unit
JVM
1.0

currentStackTrace

Returns an array of stack trace elements, each representing one stack frame. The first element of the array (assuming the array is not empty) represents the top of the stack, which is the place where currentStackTrace function was called from.

fun currentStackTrace(): Array<StackTraceElement!>!
Common
JVM
JS
Native
1.0

expect

Asserts that given function block returns the given expected value.

fun <T> expect(expected: T, block: () -> T): Unit

Asserts that given function block returns the given expected value and use the given message if it fails.

fun <T> expect(
    expected: T,
    message: String?,
    block: () -> T
): Unit
JVM
1.0

expectNoInline

fun <T> expectNoInline(expected: T, block: () -> T): Unit
fun <T> expectNoInline(
    expected: T,
    message: String?,
    block: () -> T
): Unit
Common
JVM
JS
Native
1.0

fail

Marks a test as having failed if this point in the execution path is reached, with an optional message.

fun fail(message: String? = null): Nothing

Marks a test as having failed if this point in the execution path is reached, with an optional message and cause exception.

fun fail(
    message: String? = null,
    cause: Throwable? = null
): Nothing
Common
JVM
JS
Native
1.0

todo

Takes the given block of test code and doesn't execute it.

fun todo(block: () -> Unit): Unit