How to use ConfigurationInContextInterceptor class of io.kotest.engine.spec.interceptor package

Best Kotest code snippet using io.kotest.engine.spec.interceptor.ConfigurationInContextInterceptor

SpecExecutor.kt

Source:SpecExecutor.kt Github

copy

Full Screen

...13import io.kotest.engine.interceptors.EngineContext14import io.kotest.engine.interceptors.toProjectContext15import io.kotest.engine.listener.TestEngineListener16import io.kotest.engine.spec.interceptor.ApplyExtensionsInterceptor17import io.kotest.engine.spec.interceptor.ConfigurationInContextInterceptor18import io.kotest.engine.spec.interceptor.EnabledIfSpecInterceptor19import io.kotest.engine.spec.interceptor.FinalizeSpecInterceptor20import io.kotest.engine.spec.interceptor.IgnoreNestedSpecStylesInterceptor21import io.kotest.engine.spec.interceptor.IgnoredSpecInterceptor22import io.kotest.engine.spec.interceptor.PrepareSpecInterceptor23import io.kotest.engine.spec.interceptor.ProjectContextInterceptor24import io.kotest.engine.spec.interceptor.RequiresTagSpecInterceptor25import io.kotest.engine.spec.interceptor.SpecExtensionInterceptor26import io.kotest.engine.spec.interceptor.SpecFilterInterceptor27import io.kotest.engine.spec.interceptor.SpecFinishedInterceptor28import io.kotest.engine.spec.interceptor.SpecRefExtensionInterceptor29import io.kotest.engine.spec.interceptor.SpecRefInterceptor30import io.kotest.engine.spec.interceptor.SpecStartedInterceptor31import io.kotest.engine.spec.interceptor.SystemPropertySpecFilterInterceptor32import io.kotest.engine.spec.interceptor.TagsExcludedSpecInterceptor33import io.kotest.mpp.Logger34import io.kotest.mpp.bestName35import kotlin.reflect.KClass36/**37 * Executes a single [SpecRef].38 *39 * Uses a [TestEngineListener] to notify of events in the spec lifecycle.40 *41 * The spec executor has two levels of interceptors:42 * [io.kotest.engine.spec.interceptor.SpecRefInterceptor] are executed before the spec is created.43 * [io.kotest.engine.spec.interceptor.SpecInterceptor] are executed after the spec is created.44 *45 */46@ExperimentalKotest47class SpecExecutor(48 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,49 private val context: EngineContext,50) {51 private val logger = Logger(SpecExecutorDelegate::class)52 private val extensions = SpecExtensions(context.configuration.registry)53 private val listener = context.listener54 suspend fun execute(ref: SpecRef) {55 logger.log { Pair(ref.kclass.bestName(), "Received $ref") }56 referenceInterceptors(ref)57 }58 suspend fun execute(kclass: KClass<out Spec>) {59 execute(SpecRef.Reference(kclass))60 }61 private suspend fun referenceInterceptors(ref: SpecRef) {62 val interceptors = listOfNotNull(63 if (platform == Platform.JVM) EnabledIfSpecInterceptor(listener, context.configuration.registry) else null,64 IgnoredSpecInterceptor(listener, context.configuration.registry),65 SpecFilterInterceptor(listener, context.configuration.registry),66 SystemPropertySpecFilterInterceptor(listener, context.configuration.registry),67 TagsExcludedSpecInterceptor(listener, context.configuration),68 if (platform == Platform.JVM) RequiresTagSpecInterceptor(listener, context.configuration, context.configuration.registry) else null,69 SpecRefExtensionInterceptor(context.configuration.registry),70 SpecStartedInterceptor(listener),71 SpecFinishedInterceptor(listener),72 if (platform == Platform.JVM) ApplyExtensionsInterceptor(context.configuration.registry) else null,73 PrepareSpecInterceptor(context.configuration.registry),74 FinalizeSpecInterceptor(context.configuration.registry),75 )76 val innerExecute: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> = {77 createInstance(ref).flatMap { specInterceptors(it) }78 }79 logger.log { Pair(ref.kclass.bestName(), "Executing ${interceptors.size} reference interceptors") }80 interceptors.foldRight(innerExecute) { ext: SpecRefInterceptor, fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> ->81 { ref -> ext.intercept(ref, fn) }82 }.invoke(ref)83 }84 private suspend fun specInterceptors(spec: Spec): Result<Map<TestCase, TestResult>> {85 val interceptors = listOfNotNull(86 if (platform == Platform.JS) IgnoreNestedSpecStylesInterceptor(listener, context.configuration.registry) else null,87 ProjectContextInterceptor(context.toProjectContext()),88 SpecExtensionInterceptor(context.configuration.registry),89 ConfigurationInContextInterceptor(context.configuration),90 )91 val initial: suspend (Spec) -> Result<Map<TestCase, TestResult>> = {92 try {93 val delegate = createSpecExecutorDelegate(listener, defaultCoroutineDispatcherFactory, context.configuration)94 logger.log { Pair(spec::class.bestName(), "delegate=$delegate") }95 Result.success(delegate.execute(spec))96 } catch (t: Throwable) {97 logger.log { Pair(spec::class.bestName(), "Error executing spec $t") }98 Result.failure(t)99 }100 }101 logger.log { Pair(spec::class.bestName(), "Executing ${interceptors.size} spec interceptors") }102 return interceptors.foldRight(initial) { ext, fn ->103 { spec -> ext.intercept(spec, fn) }...

Full Screen

Full Screen

ConfigurationInContextInterceptorTest.kt

Source:ConfigurationInContextInterceptorTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.spec.interceptor2import io.kotest.core.config.ProjectConfiguration3import io.kotest.core.config.configuration4import io.kotest.core.spec.style.FunSpec5import io.kotest.engine.spec.interceptor.ConfigurationInContextInterceptor6import io.kotest.matchers.booleans.shouldBeTrue7import io.kotest.matchers.shouldBe8import kotlin.coroutines.coroutineContext9class ConfigurationInContextInterceptorTest : FunSpec() {10 init {11 val c = ProjectConfiguration()12 suspend fun testConfig() {13 coroutineContext.configuration shouldBe c14 }15 test("config should be injected into the test context") {16 var fired = false17 ConfigurationInContextInterceptor(c).intercept(DummySpec()) {18 testConfig()19 fired = true20 Result.success(emptyMap())21 }22 fired.shouldBeTrue()23 }24 }25}26private class DummySpec : FunSpec()...

Full Screen

Full Screen

ConfigurationInContextInterceptor.kt

Source:ConfigurationInContextInterceptor.kt Github

copy

Full Screen

...8/**9 * A [SpecInterceptor] that injects the [ProjectConfiguration] into the coroutine context10 * so it can be extracted in specs and tests.11 */12class ConfigurationInContextInterceptor(13 private val projectConfiguration: ProjectConfiguration14) : SpecInterceptor {15 override suspend fun intercept(16 spec: Spec,17 fn: suspend (Spec) -> Result<Map<TestCase, TestResult>>18 ): Result<Map<TestCase, TestResult>> {19 return withContext(ConfigurationContextElement(projectConfiguration)) {20 fn(spec)21 }22 }23}...

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