How to use TestDispatcherInterceptor class of io.kotest.engine.test.interceptors package

Best Kotest code snippet using io.kotest.engine.test.interceptors.TestDispatcherInterceptor

TestCaseExecutor.kt

Source:TestCaseExecutor.kt Github

copy

Full Screen

...19import 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) }...

Full Screen

Full Screen

TestDispatcherInterceptor.kt

Source:TestDispatcherInterceptor.kt Github

copy

Full Screen

...18 * If the current dispatcher is already a [TestDispatcher] then this interceptor is a no-op.19 */20@ExperimentalCoroutinesApi21@ExperimentalStdlibApi22actual class TestDispatcherInterceptor : TestExecutionInterceptor {23 private val logger = Logger(TestDispatcherInterceptor::class)24 override suspend fun intercept(25 testCase: TestCase,26 scope: TestScope,27 test: suspend (TestCase, TestScope) -> TestResult28 ): TestResult {29 return when (coroutineContext[CoroutineDispatcher]) {30 is TestDispatcher -> test(testCase, scope)31 else -> {32 val dispatcher = UnconfinedTestDispatcher()33 logger.log { Pair(testCase.name.testName, "Switching context to StandardTestDispatcher: $dispatcher") }34 withContext(dispatcher + CoroutineName("wibble")) {35 test(testCase, scope.withCoroutineContext(dispatcher))36 }37 }...

Full Screen

Full Screen

TestCoroutineDispatcherInterceptorTest.kt

Source:TestCoroutineDispatcherInterceptorTest.kt Github

copy

Full Screen

...7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestResult9import io.kotest.core.test.TestType10import io.kotest.core.test.testCoroutineScheduler11import io.kotest.engine.test.interceptors.TestDispatcherInterceptor12import io.kotest.engine.test.scopes.NoopTestScope13import io.kotest.matchers.booleans.shouldBeTrue14import io.kotest.matchers.nulls.shouldNotBeNull15import kotlinx.coroutines.ExperimentalCoroutinesApi16import kotlin.time.Duration.Companion.milliseconds17@ExperimentalStdlibApi18@ExperimentalCoroutinesApi19class TestCoroutineDispatcherInterceptorTest : FunSpec() {20 init {21 test("TestCoroutineDispatcherInterceptor should install a DelayController") {22 val tc = TestCase(23 InvocationCountCheckInterceptorTest::class.toDescriptor().append("foo"),24 TestName("foo"),25 InvocationCountCheckInterceptorTest(),26 {},27 sourceRef(),28 TestType.Test,29 )30 var fired = false31 TestDispatcherInterceptor().intercept(tc, NoopTestScope(tc, coroutineContext)) { _, context ->32 context.testCoroutineScheduler.shouldNotBeNull()33 fired = true34 TestResult.Success(0.milliseconds)35 }36 fired.shouldBeTrue()37 }38 }39}

Full Screen

Full Screen

TestDispatcherInterceptor

Using AI Code Generation

copy

Full Screen

1val listener = object : TestEngineListener {2override fun engineFinished(t: List<Throwable>) {3println("Engine finished with $t")4}5override fun engineStarted(classes: List<KClass<*>>) {6println("Engine started with $classes")7}8override fun specFinished(kclass: KClass<*>, t: Throwable?, results: Map<TestCase, TestResult>) {9println("Spec finished with $t")10}11override fun specStarted(kclass: KClass<*>) {12println("Spec started with $kclass")13}14}15val engine = TestEngine(listOf(TestDispatcherInterceptor()))16engine.execute(listOf(MySpec::class), listener)

Full Screen

Full Screen

TestDispatcherInterceptor

Using AI Code Generation

copy

Full Screen

1 interceptors(TestDispatcherInterceptor)2 interceptors(TestCoroutineDispatcherInterceptor)3 interceptors(TestCoroutineExceptionHandlerInterceptor)4 interceptors(TestCoroutineScopeInterceptor)5 interceptors(TestDslStateInterceptor)6 interceptors(TestTimeoutInterceptor)7 interceptors(TestWithConfigInterceptor)8 interceptors(TestWithExtensionsInterceptor)9 interceptors(TestWithInvocationInterceptor)10 interceptors(TestWithLifecycleInterceptor)11 interceptors(TestWithProjectConfigInterceptor)12 interceptors(TestWithTestNameInterceptor)13 interceptors(TestWithTestCaseConfigInterceptor)14 interceptors(TestWithTestContextInterceptor)15 interceptors(TestWithTestDslStateInterceptor)16 interceptors(TestWithTestScopeInterceptor)

Full Screen

Full Screen

TestDispatcherInterceptor

Using AI Code Generation

copy

Full Screen

1val engine = TestEngineLauncher() 2.withTestEngine(KotestEngine()) 3.withInterceptor(TestDispatcherInterceptor()) 4.withListener(TestEngineListener()) 5.build() 6engine.execute(listOf(MySpec::class))

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful