Best Kotest code snippet using io.kotest.engine.spec.interceptor.SystemPropertySpecFilterInterceptor.filter
SpecExecutor.kt
Source: SpecExecutor.kt
1package io.kotest.engine.spec2import io.kotest.common.ExperimentalKotest3import io.kotest.common.Platform4import io.kotest.common.flatMap5import io.kotest.common.platform6import io.kotest.core.concurrency.CoroutineDispatcherFactory7import io.kotest.core.config.ProjectConfiguration8import io.kotest.core.spec.DslDrivenSpec9import io.kotest.core.spec.Spec10import io.kotest.core.spec.SpecRef11import io.kotest.core.test.TestCase12import io.kotest.core.test.TestResult13import 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) }104 }.invoke(spec)105 }106 /**107 * Creates an instance of the given [SpecRef], notifies users of the instantiation event108 * or instantiation failure, and returns a Result with the error or spec.109 *110 * After this method is called the spec is sealed.111 */112 private suspend fun createInstance(ref: SpecRef): Result<Spec> =113 ref.instance(context.configuration.registry)114 .onFailure { extensions.specInstantiationError(ref.kclass, it) }115 .flatMap { spec -> extensions.specInstantiated(spec).map { spec } }116 .onSuccess { if (it is DslDrivenSpec) it.seal() }117}118interface SpecExecutorDelegate {119 suspend fun execute(spec: Spec): Map<TestCase, TestResult>120}121@ExperimentalKotest122internal expect fun createSpecExecutorDelegate(123 listener: TestEngineListener,124 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,125 configuration: ProjectConfiguration,126): SpecExecutorDelegate...
SystemPropertySpecFilterInterceptor.kt
1package io.kotest.engine.spec.interceptor2import io.kotest.common.KotestInternal3import io.kotest.common.flatMap4import io.kotest.core.config.ExtensionRegistry5import io.kotest.core.filter.SpecFilter6import io.kotest.core.filter.SpecFilterResult7import io.kotest.core.internal.KotestEngineProperties8import io.kotest.core.spec.SpecRef9import io.kotest.core.test.TestCase10import io.kotest.core.test.TestResult11import io.kotest.engine.listener.TestEngineListener12import io.kotest.engine.spec.SpecExtensions13import io.kotest.mpp.Logger14import io.kotest.mpp.bestName15import io.kotest.mpp.syspropOrEnv16import kotlin.reflect.KClass17/**18 * Applies spec filters using sysprop or env vars from [KotestEngineProperties.filterSpecs].19 *20 * These work similarly to gradle filters in --tests described at:21 * https://docs.gradle.org/current/userguide/java_testing.html#full_qualified_name_pattern22 */23@OptIn(KotestInternal::class)24internal class SystemPropertySpecFilterInterceptor(25 private val listener: TestEngineListener,26 registry: ExtensionRegistry27) : SpecRefInterceptor {28 private val logger = Logger(SystemPropertySpecFilterInterceptor::class)29 private val extensions = SpecExtensions(registry)30 override suspend fun intercept(31 ref: SpecRef,32 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>33 ): Result<Map<TestCase, TestResult>> {34 val filter = syspropOrEnv(KotestEngineProperties.filterSpecs) ?: ""35 logger.log { Pair(ref.kclass.bestName(), "Filter specs syspropOrEnv=$filter") }36 val included = filter37 .propertyToRegexes()38 .map { it.toSpecFilter() }39 .all { it.filter(ref.kclass) == SpecFilterResult.Include }40 logger.log { Pair(ref.kclass.bestName(), "included = $included") }41 return if (included) {42 fn(ref)43 } else {44 runCatching { listener.specIgnored(ref.kclass, "Filtered by ${KotestEngineProperties.filterSpecs} spec filter") }45 .flatMap { extensions.ignored(ref.kclass, "Filtered by ${KotestEngineProperties.filterSpecs} spec filter") }46 .map { emptyMap() }47 }48 }49}50private fun Regex.toSpecFilter(): SpecFilter = object : SpecFilter {51 override fun filter(kclass: KClass<*>): SpecFilterResult {52 val name = kclass.bestName()53 return if (this@toSpecFilter.matches(name)) SpecFilterResult.Include else SpecFilterResult.Exclude("Disabled by spec filter: $this")54 }55}56private fun String.propertyToRegexes(): List<Regex> =57 this.split(",")58 .filter { it.isNotBlank() }59 .map { it.replace("*", ".*?").toRegex() }...
filter
Using AI Code Generation
1val filter = SystemPropertySpecFilterInterceptor()2val specs = filter.filter(listOf("com.example.FooSpec", "com.example.BarSpec"))3val filter = SystemPropertyTestFilterInterceptor()4val tests = filter.filter(listOf("com.example.FooSpec" to "test 1", "com.example.FooSpec" to "test 2"))5val filter = SystemPropertyTestPathFilterInterceptor()6val tests = filter.filter(listOf("com.example.FooSpec" to "test 1", "com.example.FooSpec" to "test 2"))7val filter = SystemPropertyTagFilterInterceptor()8val specs = filter.filter(listOf("com.example.FooSpec", "com.example.BarSpec"))9val filter = SystemPropertyTestCaseFilterInterceptor()10val tests = filter.filter(listOf("com.example.FooSpec" to "test 1", "com.example.FooSpec" to "test 2"))11val filter = SystemPropertyTestNameFilterInterceptor()12val tests = filter.filter(listOf("com.example.FooSpec" to "test 1", "com.example.FooSpec" to "test 2"))13val filter = SystemPropertyTestPathNameFilterInterceptor()14val tests = filter.filter(listOf("com.example.FooSpec" to "test 1", "com.example.FooSpec" to "test 2"))15val filter = SystemPropertyTestPathScopeFilterInterceptor()16val tests = filter.filter(listOf("com.example.FooSpec" to "test 1", "com.example.FooSpec" to "test 2"))
filter
Using AI Code Generation
1val specFilter = SystemPropertySpecFilterInterceptor()2val filter = specFilter.filter()3val engine = TestEngineLauncher()4.engine("kotest")5.filters(listOf(filter))6.suite("com.test")7.execute()8val tagFilter = SystemPropertyTagFilterInterceptor()9val filter = tagFilter.filter()10val engine = TestEngineLauncher()11.engine("kotest")12.filters(listOf(filter))13.suite("com.test")14.execute()15val projectFilter = SystemPropertyProjectFilterInterceptor()16val filter = projectFilter.filter()17val engine = TestEngineLauncher()18.engine("kotest")19.filters(listOf(filter))20.suite("com.test")21.execute()22val excludeTagFilter = SystemPropertyExcludeTagsFilterInterceptor()23val filter = excludeTagFilter.filter()24val engine = TestEngineLauncher()25.engine("kotest")26.filters(listOf(filter))27.suite("com.test")28.execute()29val excludeProjectFilter = SystemPropertyExcludeProjectFilterInterceptor()30val filter = excludeProjectFilter.filter()31val engine = TestEngineLauncher()32.engine("kotest")33.filters(listOf(filter))34.suite("com.test")35.execute()36val excludeSpecFilter = SystemPropertyExcludeSpecFilterInterceptor()37val filter = excludeSpecFilter.filter()38val engine = TestEngineLauncher()39.engine("kotest")40.filters(listOf(filter))41.suite("com.test")42.execute()43val excludeTestFilter = SystemPropertyExcludeTestFilterInterceptor()44val filter = excludeTestFilter.filter()45val engine = TestEngineLauncher()46.engine("kotest")47.filters(listOf(filter))48.suite("com.test")49.execute()50val excludeTagAndSpecFilter = SystemPropertyExcludeTagsAndSpecFilterInterceptor()51val filter = excludeTagAndSpecFilter.filter()
Check out the latest blogs from LambdaTest on this topic:
Agile software development stems from a philosophy that being agile means creating and responding to change swiftly. Agile means having the ability to adapt and respond to change without dissolving into chaos. Being Agile involves teamwork built on diverse capabilities, skills, and talents. Team members include both the business and software development sides working together to produce working software that meets or exceeds customer expectations continuously.
When working on web automation with Selenium, I encountered scenarios where I needed to refresh pages from time to time. When does this happen? One scenario is that I needed to refresh the page to check that the data I expected to see was still available even after refreshing. Another possibility is to clear form data without going through each input individually.
Building a website is all about keeping the user experience in mind. Ultimately, it’s about providing visitors with a mind-blowing experience so they’ll keep coming back. One way to ensure visitors have a great time on your site is to add some eye-catching text or image animations.
Automating testing is a crucial step in the development pipeline of a software product. In an agile development environment, where there is continuous development, deployment, and maintenance of software products, automation testing ensures that the end software products delivered are error-free.
As a developer, checking the cross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. ????
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!!