kotest-assertions / io.kotest.assertions

Package io.kotest.assertions

Types

Actual

data class Actual

AssertionCounter

object AssertionCounter

ErrorCollectionMode

enum class ErrorCollectionMode

ErrorCollector

object ErrorCollector

Expected

data class Expected

Failures

object Failures

UserStackTraceConverter

object UserStackTraceConverter

Exceptions

MultiAssertionError

An error that bundles multiple other Throwables together

class MultiAssertionError : AssertionError

Extensions for External Classes

kotlin.collections.Iterable

Functions

asClue

Similar to withClue, but will add this as a clue to the assertion error message in case an assertion fails. Can be nested, the error message will contain all available clues.

fun <T : Any, R> T.asClue(block: (T) -> R): R

assertSoftly

Run multiple assertions and throw a single error after all are executed if any fail

fun <T> assertSoftly(assertions: () -> T): T

cleanStackTrace

Removes io.kotest stack elements from the given throwable if the platform supports stack traces.

fun <T : Throwable> cleanStackTrace(throwable: T): T

clueContextAsString

fun clueContextAsString(): String

collectOrThrow

If we are in "soft assertion mode" will add this throwable to the list of throwables for the current execution. Otherwise will throw immediately.

fun ErrorCollector.collectOrThrow(error: Throwable): Unit

createAssertionError

Creates an AssertionError from the given message. If the platform supports nested exceptions, the cause is set to the given cause. If the platform supports stack traces, then the stack is cleaned of io.kotest lines.

fun createAssertionError(message: String, cause: Throwable?): AssertionError

Creates the best error type supported on the platform (eg opentest4j.AssertionFailedException) from the given message and expected and actual values. If the platform supports nested exceptions, the cause is set to the given cause.

fun createAssertionError(message: String, cause: Throwable?, expected: Expected, actual: Actual): Throwable

diffLargeString

Relies on a JVM only package, so on JS etc will return no diff.

fun diffLargeString(expected: String, actual: String): Pair<String, String>

exceptionToMessage

Returns a string error message from the given throwable. If the type is an AssertionError then the message is taken from the exceptions own message, otherwise the exception is converted to a string.

fun exceptionToMessage(t: Throwable): String

extracting

extracting pulls property values out of a list of objects for typed bulk assertions on properties.

fun <K, T> extracting(col: Collection<K>, extractor: K.() -> T): List<T>

fail

fun fail(msg: String): Nothing

failure

Creates the most appropriate error from the given message, wrapping in clue context(s) if any are set.

fun failure(message: String): AssertionError

Creates an AssertionError from the given message, wrapping in clue context(s) if any are set, and setting the cause as cause on platforms that supported nested exceptions.

fun failure(message: String, cause: Throwable?): AssertionError

Creates a Throwable from expected and actual values, appending clue context(s) if any are set. The error message is generated in the intellij 'diff' format.

fun failure(expected: Expected, actual: Actual): Throwable

getAndReset

fun AssertionCounter.getAndReset(): Int

inspecting

Inspecting allows to assert the properties of an object in a typed fashion providing a proper testing context.

fun <K> inspecting(obj: K, inspector: K.() -> Unit): Unit

intellijFormatError

Returns a message formatted appropriately for intellij to show a diff.

fun intellijFormatError(expected: Expected, actual: Actual): String

junit4ComparisonFailure

If JUnit4 is present, return a org.junit.ComparisonFailure otherwise returns None.

fun junit4ComparisonFailure(message: String, expected: Expected, actual: Actual): Option<Throwable>

junit5AssertionFailedError

If JUnit5 is present, return an org.opentest4j.AssertionFailedError using the given message and expeted and actual values, otherwise returns None.

fun junit5AssertionFailedError(message: String, expected: Expected, actual: Actual, cause: Throwable?): Option<Throwable>

multiAssertionError

fun multiAssertionError(errors: List<Throwable>): Throwable

retry

Retry the given lambda f in case of assertion errorAssertionError and exception Exception. Stops trying when maxRetry or timeout is reached.

suspend fun <T> retry(maxRetry: Int, timeout: Duration, delay: Duration = timeout, multiplier: Int = 1, f: () -> T): T

Retry the given lambda f in case of assertion errorAssertionError and exception of type exceptionClass. Stops trying when maxRetry or timeout is reached. Or an exception other than exceptionClass is thrown

suspend fun <T, E : Throwable> retry(maxRetry: Int, timeout: Duration, delay: Duration = timeout, multiplier: Int = 1, exceptionClass: KClass<E>, f: () -> T): T

shouldFail

Verifies that block throws an AssertionError

fun shouldFail(block: () -> Any?): AssertionError

supportsStringDiff

fun supportsStringDiff(): Boolean

throwCollectedErrors

The errors for the current execution are thrown as a single throwable.

fun ErrorCollector.throwCollectedErrors(): Unit

withClue

Add clue as additional info to the assertion error message in case an assertion fails. Can be nested, the error message will contain all available clues.

fun <R> withClue(clue: Any, thunk: () -> R): R