Best Kotest code snippet using io.kotest.engine.test.interceptors.CoroutineErrorCollectorInterceptor
TestCaseExecutor.kt
Source:TestCaseExecutor.kt
1@file:OptIn(ExperimentalStdlibApi::class)2package io.kotest.engine.test3import io.kotest.common.ExperimentalKotest4import io.kotest.common.Platform5import io.kotest.common.platform6import io.kotest.core.concurrency.CoroutineDispatcherFactory7import io.kotest.core.config.ProjectConfiguration8import io.kotest.core.test.TestCase9import io.kotest.core.test.TestResult10import io.kotest.core.test.TestScope11import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory12import io.kotest.engine.test.interceptors.AssertionModeInterceptor13import io.kotest.engine.test.interceptors.CoroutineDebugProbeInterceptor14import io.kotest.engine.test.interceptors.CoroutineLoggingInterceptor15import io.kotest.engine.test.interceptors.EnabledCheckInterceptor16import io.kotest.engine.test.interceptors.InvocationCountCheckInterceptor17import io.kotest.engine.test.interceptors.InvocationTimeoutInterceptor18import io.kotest.engine.test.interceptors.LifecycleInterceptor19import io.kotest.engine.test.interceptors.SoftAssertInterceptor20import io.kotest.engine.test.interceptors.SupervisorScopeInterceptor21import io.kotest.engine.test.interceptors.TestCaseExtensionInterceptor22import io.kotest.engine.test.interceptors.TestCoroutineInterceptor23import io.kotest.engine.test.interceptors.TestDispatcherInterceptor24import io.kotest.engine.test.interceptors.TestFinishedInterceptor25import io.kotest.engine.test.interceptors.TestPathContextInterceptor26import io.kotest.engine.test.interceptors.TimeoutInterceptor27import io.kotest.engine.test.interceptors.blockedThreadTimeoutInterceptor28import io.kotest.engine.test.interceptors.coroutineDispatcherFactoryInterceptor29import io.kotest.engine.test.interceptors.coroutineErrorCollectorInterceptor30import io.kotest.mpp.Logger31import kotlin.time.Duration32import kotlin.time.TimeSource33/**34 * Executes a single [TestCase].35 *36 * Uses a [TestCaseExecutionListener] to notify callers of events in the test lifecycle.37 *38 */39@OptIn(ExperimentalKotest::class)40class TestCaseExecutor(41 private val listener: TestCaseExecutionListener,42 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory = NoopCoroutineDispatcherFactory,43 private val configuration: ProjectConfiguration,44) {45 private val logger = Logger(TestCaseExecutor::class)46 suspend fun execute(testCase: TestCase, testScope: TestScope): TestResult {47 val timeMark = TimeSource.Monotonic.markNow()48 val interceptors = listOfNotNull(49 TestPathContextInterceptor,50 TestFinishedInterceptor(listener),51 InvocationCountCheckInterceptor,52 SupervisorScopeInterceptor,53 if (platform == Platform.JVM) coroutineDispatcherFactoryInterceptor(defaultCoroutineDispatcherFactory) else null,54 if (platform == Platform.JVM) coroutineErrorCollectorInterceptor() else null,55 TestCaseExtensionInterceptor(configuration.registry),56 EnabledCheckInterceptor(configuration),57 LifecycleInterceptor(listener, timeMark, configuration.registry),58 AssertionModeInterceptor,59 SoftAssertInterceptor(),60 CoroutineLoggingInterceptor(configuration),61 if (platform == Platform.JVM) blockedThreadTimeoutInterceptor(configuration, timeMark) else null,62 TimeoutInterceptor(timeMark),63 TestInvocationInterceptor(configuration.registry, timeMark),64 InvocationTimeoutInterceptor,65 if (platform == Platform.JVM && testCase.config.testCoroutineDispatcher) TestDispatcherInterceptor() else null,66 if (platform == Platform.JVM && testCase.config.coroutineTestScope) TestCoroutineInterceptor() else null,67 CoroutineDebugProbeInterceptor,68 )69 val innerExecute: suspend (TestCase, TestScope) -> TestResult = { tc, scope ->70 logger.log { Pair(testCase.name.testName, "Executing test") }71 tc.test(scope)72 try {73 TestResult.Success(timeMark.elapsedNow())74 } catch (e: Throwable) {75 TestResult.Success(Duration.ZERO) // workaround for kotlin 1.576 }77 }78 return interceptors.foldRight(innerExecute) { ext, fn ->79 { tc, sc -> ext.intercept(tc, sc, fn) }80 }.invoke(testCase, testScope)81 }82}...
CoroutineErrorCollectorInterceptor.kt
Source:CoroutineErrorCollectorInterceptor.kt
...5import io.kotest.core.test.TestScope6import io.kotest.mpp.Logger7import kotlinx.coroutines.withContext8internal actual fun coroutineErrorCollectorInterceptor(): TestExecutionInterceptor =9 CoroutineErrorCollectorInterceptor10/**11 * A [TestExecutionInterceptor] for keeping the error collector synchronized with thread-switching coroutines.12 * Note: This is a JVM only option.13 */14internal object CoroutineErrorCollectorInterceptor : TestExecutionInterceptor {15 private val logger = Logger(CoroutineErrorCollectorInterceptor::class)16 override suspend fun intercept(17 testCase: TestCase,18 scope: TestScope,19 test: suspend (TestCase, TestScope) -> TestResult20 ): TestResult {21 logger.log { Pair(testCase.name.testName, "Adding $errorCollectorContextElement to coroutine context") }22 return withContext(errorCollectorContextElement) {23 test(testCase, scope)24 }25 }26}...
interceptors.kt
Source:interceptors.kt
1package io.kotest.engine.test.interceptors2import io.kotest.common.platform3import io.kotest.core.concurrency.CoroutineDispatcherFactory4import io.kotest.core.config.ProjectConfiguration5import kotlin.time.TimeMark6internal actual fun coroutineDispatcherFactoryInterceptor(7 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory8): TestExecutionInterceptor = error("Unsupported on $platform")9internal actual fun blockedThreadTimeoutInterceptor(10 configuration: ProjectConfiguration,11 start: TimeMark,12): TestExecutionInterceptor = error("Unsupported on $platform")13internal actual fun coroutineErrorCollectorInterceptor(): TestExecutionInterceptor =14 error("Unsupported on $platform")...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!