Package kotlin.test

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

Types

Asserter

interface Asserter

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

AsserterContributor

interface AsserterContributor

Checks applicability and provides Asserter instance

AssertionResult

external interface AssertionResult

Describes the result of an assertion execution.

CollectionAssertionSession

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

DefaultAsserter

class DefaultAsserter : Asserter

Default Asserter implementation to avoid dependency on JUnit or TestNG.

FrameworkAdapter

external interface FrameworkAdapter

Serves as a bridge to a testing framework.

Annotations

AfterTest

annotation actual class AfterTest

Marks a function to be invoked after each test.

BeforeTest

annotation actual class BeforeTest

Marks a function to be invoked before each test.

Ignore

annotation actual class Ignore

Marks a test or a suite as ignored.

Test

annotation actual class Test

Marks a function as a test.

Type Aliases

AfterTest

actual typealias AfterTest = After

BeforeTest

actual typealias BeforeTest = Before

Ignore

actual typealias Ignore = Ignore

Test

actual typealias Test = Test

Properties

asserter

val asserter: Asserter

Current adapter providing assertion implementations

Functions

assert

fun <E, C : Iterable<E>> assert(
    collection: C,
    block: CollectionAssertionSession<E, C>.() -> Unit
): Unit

assertEquals

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

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

assertFails

fun assertFails(block: () -> Unit): Throwable
fun assertFails(
    message: String?,
    block: () -> Unit
): Throwable

Asserts that given function block fails by throwing an exception.

assertFailsWith

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

Asserts that a block fails with a specific exception of type T being thrown. Since inline method doesn't allow to trace where it was invoked, it is required to pass a message to distinguish this method call from others.

fun <T : Throwable> assertFailsWith(
    exceptionClass: KClass<T>,
    block: () -> Unit
): T
expect fun <T : Throwable> assertFailsWith(
    exceptionClass: KClass<T>,
    message: String?,
    block: () -> Unit
): T

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

assertFalse

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

Asserts that the given block returns false.

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

Asserts that the expression is false with an optional message.

assertNotEquals

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

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

assertNotNull

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

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

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

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

assertNotSame

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

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

assertNull

fun assertNull(actual: Any?, message: String? = null): Unit

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

assertSame

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

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

assertTrue

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

Asserts that the given block returns true.

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

Asserts that the expression is true with an optional message.

containsAll

fun <T> CollectionAssertionSession<T, *>.containsAll(
    vararg elements: T
): Unit

currentStackTrace

fun currentStackTrace(): Array<StackTraceElement>

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.

elementAtShouldBe

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

elementAtShouldComply

fun <T, C : Iterable<T>> CollectionAssertionSession<T, C>.elementAtShouldComply(
    position: Int,
    message: String? = null,
    predicate: (T) -> Boolean
): Unit

expect

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

Asserts that given function block returns the given expected value.

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

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

fail

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.

lastElementShouldBe

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

shouldBe

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

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

sizeShouldBe

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

todo

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

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