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
JVM
1.0

CollectionAssertionSession

class CollectionAssertionSession<E, C : Iterable<E>>

DefaultAsserter

Default Asserter implementation to avoid dependency on JUnit or TestNG.

Common
JVM
JS
1.0
class DefaultAsserter : Asserter
Native
1.3
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
Native
1.3

AfterEach

Marks a function to be executed after a test.

annotation class AfterEach

AfterTest

Marks a function to be invoked after each test.

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

BeforeClass

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

annotation class BeforeClass
Native
1.3

BeforeEach

Marks a function to be executed before a test.

annotation class BeforeEach

BeforeTest

Marks a function to be invoked before each test.

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

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

JVM
1.0

assert

fun <E, C : Iterable<E>> assert(
    collection: C,
    block: CollectionAssertionSession<E, C>.() -> Unit
): 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
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
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
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
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

containsAll

fun <T> CollectionAssertionSession<T, *>.containsAll(
    vararg elements: T
): 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!>!
JVM
1.0

elementAtShouldBe

fun <T> CollectionAssertionSession<T, *>.elementAtShouldBe(
    position: Int,
    expected: T,
    message: String? = null
): Unit
JVM
1.0

elementAtShouldComply

fun <T, C : Iterable<T>> CollectionAssertionSession<T, C>.elementAtShouldComply(
    position: Int,
    message: String? = null,
    predicate: (T) -> Boolean
): Unit
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
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
JVM
1.0

lastElementShouldBe

fun <T> CollectionAssertionSession<T, *>.lastElementShouldBe(
    expected: T,
    message: String? = null
): Unit
JVM
1.0

shouldBe

fun <T, C : Iterable<T>> CollectionAssertionSession<T, C>.shouldBe(
    expectedElements: Iterable<T>,
    message: String? = null
): Unit
JVM
1.0

shouldBeSet

fun <T, C : Set<T>> CollectionAssertionSession<T, C>.shouldBeSet(
    other: Set<T>,
    message: String? = null
): Unit
fun <T, C : Set<T>> CollectionAssertionSession<T, C>.shouldBeSet(
    vararg other: T
): Unit
JVM
1.0

sizeShouldBe

fun <C : Collection<*>> CollectionAssertionSession<*, C>.sizeShouldBe(
    expectedSize: Int,
    message: String? = null
): Unit
Common
JVM
JS
Native
1.0

todo

Comments out a block of test code until it is implemented while keeping a link to the code to implement in your unit test output

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