Best Kotest code snippet using io.kotest.engine.spec.interceptor.SpecFilterInterceptor
SpecExecutor.kt
Source:SpecExecutor.kt
...22import 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>> ->...
SpecFilterInterceptor.kt
Source:SpecFilterInterceptor.kt
...11import io.kotest.mpp.bestName12/**13 * Evaluates a spec against any registered [SpecFilter]s.14 */15class SpecFilterInterceptor(16 private val listener: TestEngineListener,17 private val registry: ExtensionRegistry18) : SpecRefInterceptor {19 private val extensions = SpecExtensions(registry)20 private val logger = Logger(SpecFilterInterceptor::class)21 override suspend fun intercept(22 ref: SpecRef,23 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>24 ): Result<Map<TestCase, TestResult>> {25 val excluded = registry.all().filterIsInstance<SpecFilter>().mapNotNull {26 val result = it.filter(ref.kclass)27 if (result is SpecFilterResult.Exclude) result else null28 }.firstOrNull()29 logger.log { Pair(ref.kclass.bestName(), "excludedByFilters == $excluded") }30 return if (excluded == null) {31 fn(ref)32 } else {33 val reason = excluded.reason ?: "Disabled by spec filter"34 listener.specIgnored(ref.kclass, reason)...
SpecFilterInterceptorTest.kt
Source:SpecFilterInterceptorTest.kt
...4import io.kotest.core.filter.SpecFilterResult5import io.kotest.core.spec.SpecRef6import io.kotest.core.spec.style.FunSpec7import io.kotest.engine.listener.NoopTestEngineListener8import io.kotest.engine.spec.interceptor.SpecFilterInterceptor9import io.kotest.matchers.booleans.shouldBeFalse10import io.kotest.matchers.booleans.shouldBeTrue11import kotlin.reflect.KClass12class SpecFilterInterceptorTest : FunSpec() {13 init {14 test("spec filter's should filter tests") {15 val c = ProjectConfiguration()16 c.registry.add(object : SpecFilter {17 override fun filter(kclass: KClass<*>): SpecFilterResult {18 return if (kclass.simpleName == "FooSpec") SpecFilterResult.Exclude("foo") else SpecFilterResult.Include19 }20 })21 var fired = false22 SpecFilterInterceptor(NoopTestEngineListener, c.registry).intercept(SpecRef.Reference(FooSpec::class)) {23 fired = true24 Result.success(emptyMap())25 }26 fired.shouldBeFalse()27 SpecFilterInterceptor(NoopTestEngineListener, c.registry).intercept(SpecRef.Reference(BarSpec::class)) {28 fired = true29 Result.success(emptyMap())30 }31 fired.shouldBeTrue()32 }33 }34}35private class FooSpec : FunSpec()36private class BarSpec : FunSpec()
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!!