How to use ErrorCollector.pushErrorAndMaybeThrow method of io.kotest.assertions.errorAndAssertionsScope class

Best Kotest code snippet using io.kotest.assertions.errorAndAssertionsScope.ErrorCollector.pushErrorAndMaybeThrow

errorAndAssertionsScope.kt

Source:errorAndAssertionsScope.kt Github

copy

Full Screen

1package io.kotest.assertions2import io.kotest.common.ExperimentalKotest3import kotlinx.coroutines.withContext4import kotlin.coroutines.AbstractCoroutineContextElement5import kotlin.coroutines.CoroutineContext6import kotlin.coroutines.coroutineContext7internal typealias Failures = List<Throwable>8internal typealias Assertions = Int9internal class AssertionBlockContextElement : AbstractCoroutineContextElement(Key) {10 companion object Key : CoroutineContext.Key<AssertionBlockContextElement>11}12/**13 * [errorAndAssertionsScope] runs [block] in a "clean" scope.14 * The original error and assertion counts are stored and set to empty before the [block] is run.15 * Once the block is executed, the result [T], the [Failures], and the number of [Assertions] are stored for return.16 * The original error and assertion counts are replaced into their respective tracking systems [errorCollector] and [assertionCounter].17 *18 * The calling function is responsible for inserting the resultant [Failures] and [Assertions] into [errorCollector] and [assertionCounter] if appropriate.19 *20 * @return The result [T] of the block function, the [Failures] that block had, and the number of [Assertions] executed.21 */22@ExperimentalKotest23internal suspend fun <T> errorAndAssertionsScope(block: suspend () -> T): Triple<Result<T>, Failures, Assertions> {24 if (coroutineContext[AssertionBlockContextElement] != null) {25 throw IllegalStateException("Assertion block functions one, any, and all are limited to a depth of 1")26 }27 val originalFailures = errorCollector.getAndReplace(listOf())28 val originalAssertions = assertionCounter.getAndReset()29 val originalMode = errorCollector.getCollectionMode()30 errorCollector.setCollectionMode(ErrorCollectionMode.Soft)31 val result = runCatching {32 withContext(AssertionBlockContextElement()) {33 block()34 }35 }36 errorCollector.setCollectionMode(originalMode)37 val resultFailures = errorCollector.getAndReplace(originalFailures)38 val resultAssertions = assertionCounter.getAndReset()39 repeat(originalAssertions) { assertionCounter.inc() }40 return Triple(result, resultFailures, resultAssertions)41}42/**43 * Pushes the provided [error] onto the [errorCollector] and throws if the configured collection mode is [ErrorCollectionMode.Hard]44 */45@ExperimentalKotest46internal fun ErrorCollector.pushErrorAndMaybeThrow(error: Throwable) {47 pushError(error)48 if (getCollectionMode() == ErrorCollectionMode.Hard) {49 throwCollectedErrors()50 }51}...

Full Screen

Full Screen

one.kt

Source:one.kt Github

copy

Full Screen

1package io.kotest.assertions2import io.kotest.common.ExperimentalKotest3/**4 * Executes the given lambda and expects exactly one assertion to succeed.5 * If zero, two or more assertions succeed then this will cause this block to fail.6 *7 * ```8 * one {9 * "foo" shouldBe "bar"10 * "foo" shouldBe "foo11 * "foo" shouldBe "baz"12 * }13 * ```14 */15@ExperimentalKotest16suspend fun <T> one(assertions: suspend () -> T): T {17 val (result, failures, assertionsCount) = errorAndAssertionsScope { assertions() }18 assertionCounter.inc(assertionsCount)19 if (assertionsCount < 2) {20 errorCollector.collectOrThrow(failures + failure("One cannot ensure a mutual exclusion with less than two assertions"))21 }22 if (assertionsCount == failures.size + 1) {23 return result.getOrThrow()24 }25 errorCollector.pushErrors(failures)26 val f = when {27 assertionsCount == failures.size -> failure("One expected a single assertion to succeed, but none succeeded.")28 assertionsCount > failures.size + 1 -> failure("One expected a single assertion to succeed, but more than one succeeded.")29 else -> failure("One expected a single assertion to succeed, but there were more failures than assertions.")30 }31 errorCollector.pushErrorAndMaybeThrow(f)32 throw f // one/all/any won't respect softly for now33}34/**35 * Executes the given lambda and expects exactly one assertion to succeed.36 * If zero, two or more assertions suceed then this will cause an exception.37 *38 * Returns the original value [t] on success for use in subsequent assertions.39 */40@ExperimentalKotest41suspend fun <T> one(t: T, assertions: suspend T.(T) -> Unit) = one {42 t.assertions(t)43 t44}...

Full Screen

Full Screen

any.kt

Source:any.kt Github

copy

Full Screen

1package io.kotest.assertions2import io.kotest.common.ExperimentalKotest3/**4 * Runs multiple assertions and expects at least one to succeed, will suppress all exceptions otherwise.5 *6 * ```7 * any {8 * "foo" shouldBe "bar"9 * "foo" shouldBe "foo10 * "foo" shouldBe "baz"11 * }12 * ```13 */14@ExperimentalKotest15suspend fun <T> any(assertions: suspend () -> T): T {16 val (result, failures, assertionCount) = errorAndAssertionsScope { assertions() }17 assertionCounter.inc(assertionCount)18 if (assertionCount > failures.size || failures.isEmpty() && assertionCount == 0) {19 return result.getOrThrow()20 }21 val f = failure("Any expected at least one assertion to succeed but they all failed")22 errorCollector.pushErrors(failures)23 errorCollector.pushErrorAndMaybeThrow(f)24 throw f25}26@ExperimentalKotest27suspend fun <T> any(t: T, assertions: suspend T.(T) -> Unit) = any {28 t.assertions(t)29 t30}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in errorAndAssertionsScope

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful