Best Kotest code snippet using io.kotest.engine.interceptors.EngineContext.ProjectContext.toEngineContext
EngineInterceptor.kt
Source:EngineInterceptor.kt
1package io.kotest.engine.interceptors2import io.kotest.common.KotestInternal3import io.kotest.core.TagExpression4import io.kotest.core.config.ProjectConfiguration5import io.kotest.core.project.ProjectContext6import io.kotest.core.project.TestSuite7import io.kotest.engine.EngineResult8import io.kotest.engine.listener.CompositeTestEngineListener9import io.kotest.engine.listener.NoopTestEngineListener10import io.kotest.engine.listener.TestEngineListener11/**12 * Internal pipeline that intercepts calls to the engine.13 *14 * This can be used to execute code before or after the engine15 * and permits changing the [EngineContext].16 */17@KotestInternal18interface EngineInterceptor {19 suspend fun intercept(context: EngineContext, execute: suspend (EngineContext) -> EngineResult): EngineResult20}21data class EngineContext(22 val suite: TestSuite,23 val listener: TestEngineListener,24 val tags: TagExpression,25 val configuration: ProjectConfiguration,26) {27 constructor(configuration: ProjectConfiguration) : this(28 TestSuite.empty,29 NoopTestEngineListener,30 TagExpression.Empty,31 configuration32 )33 companion object {34 val empty = EngineContext(TestSuite.empty, NoopTestEngineListener, TagExpression.Empty, ProjectConfiguration())35 }36 /**37 * Returns this [EngineContext] with the given [listener] added via a [CompositeTestEngineListener].38 */39 fun mergeListener(listener: TestEngineListener): EngineContext {40 return EngineContext(suite, CompositeTestEngineListener(listOf(this.listener, listener)), tags, configuration)41 }42 fun withTestSuite(suite: TestSuite): EngineContext {43 return EngineContext(suite, listener, tags, configuration)44 }45 fun withListener(listener: TestEngineListener): EngineContext {46 return EngineContext(suite, listener, tags, configuration)47 }48 fun withConfiguration(c: ProjectConfiguration): EngineContext {49 return EngineContext(suite, listener, tags, c)50 }51 fun withTags(tags: TagExpression): EngineContext {52 return EngineContext(suite, listener, tags, configuration)53 }54}55fun ProjectContext.toEngineContext(context: EngineContext): EngineContext {56 return EngineContext(57 suite,58 context.listener,59 tags,60 configuration61 )62}63fun EngineContext.toProjectContext(): ProjectContext {64 return ProjectContext(65 suite,66 tags,67 configuration68 )69}...
ProjectExtensionEngineInterceptor.kt
Source:ProjectExtensionEngineInterceptor.kt
1package io.kotest.engine.interceptors2import io.kotest.common.KotestInternal3import io.kotest.core.extensions.ProjectExtension4import io.kotest.core.project.ProjectContext5import io.kotest.engine.EngineResult6/**7 * An [EngineInterceptor] that invokes any [ProjectExtension]s before the engine begins execution.8 *9 * Project extensions can adapt the [ProjectContext] which is the public API version of10 * the [EngineContext]. Any changes to the project context are reflected downstream in11 * the engine context passed to the execute function.12 */13@KotestInternal14internal object ProjectExtensionEngineInterceptor : EngineInterceptor {15 override suspend fun intercept(16 context: EngineContext,17 execute: suspend (EngineContext) -> EngineResult18 ): EngineResult {19 var result: EngineResult = EngineResult.empty20 val initial: suspend (ProjectContext) -> Unit = { result = execute(it.toEngineContext(context)) }21 val chain = context22 .configuration23 .registry24 .all()25 .filterIsInstance<ProjectExtension>()26 .foldRight(initial) { extension, acc: suspend (ProjectContext) -> Unit ->27 {28 extension.interceptProject(context.toProjectContext()) { acc(it) }29 }30 }31 return try {32 chain.invoke(context.toProjectContext())33 result34 } catch (t: Throwable) {35 result.addError(t)36 }37 }38}...
ProjectContext.toEngineContext
Using AI Code Generation
1import io.kotest.core.config.Project2import io.kotest.core.engine.EngineResult3import io.kotest.core.engine.TestEngine4import io.kotest.core.engine.TestSuite5import io.kotest.core.spec.Spec6import io.kotest.core.test.TestCase7import io.kotest.core.test.TestContext8import io.kotest.core.test.TestResult9import io.kotest.core.test.TestType10import io.kotest.engine.interceptors.EngineContext11import io.kotest.engine.interceptors.EngineInterceptor12import io.kotest.engine.interceptors.SpecRunnerInterceptor13import io.kotest.engine.interceptors.spec14import io.kotest.engine.listener.TestEngineListener15import io.kotest.engine.test.withCoroutineContext16import io.kotest.engine.test.withSpecCoroutineContext17import io.kotest.fp.Try18import io.kotest.mpp.log19import kotlinx.coroutines.CoroutineDispatcher20import kotlinx.coroutines.CoroutineScope21import kotlinx.coroutines.Dispatchers22import kotlinx.coroutines.async23import kotlinx.coroutines.awaitAll24import kotlinx.coroutines.coroutineScope25import kotlinx.coroutines.launch26import kotlinx.coroutines.runBlocking27import kotlinx.coroutines.withContext28import kotlin.coroutines.CoroutineContext29class TestEngine(30) : TestEngine {31 override fun execute(): EngineResult {32 log { "TestEngine: Starting engine with ${specs.size} specs" }33 val result = runBlocking(dispatcher) {34 executeSpecs(specs)35 }36 log { "TestEngine: Engine completed with result: $result" }
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!!