Best Kotest code snippet using io.kotest.engine.test.status.enabled.TestCase.isEnabledInternal
IsEnabledTest.kt
Source:IsEnabledTest.kt  
1package com.sksamuel.kotest.engine.active2import io.kotest.common.ExperimentalKotest3import io.kotest.core.NamedTag4import io.kotest.core.TagExpression5import io.kotest.core.config.ProjectConfiguration6import io.kotest.core.descriptors.Descriptor7import io.kotest.core.descriptors.append8import io.kotest.core.descriptors.toDescriptor9import io.kotest.core.extensions.EnabledExtension10import io.kotest.core.extensions.TagExtension11import io.kotest.core.filter.TestFilter12import io.kotest.core.filter.TestFilterResult13import io.kotest.core.filter.toTestFilterResult14import io.kotest.core.names.TestName15import io.kotest.core.spec.style.FunSpec16import io.kotest.core.spec.style.StringSpec17import io.kotest.core.test.Enabled18import io.kotest.core.test.TestCase19import io.kotest.core.test.TestType20import io.kotest.core.test.config.ResolvedTestConfig21import io.kotest.engine.test.status.isEnabled22import io.kotest.engine.test.status.isEnabledInternal23import io.kotest.matchers.shouldBe24@ExperimentalKotest25class IsEnabledTest : StringSpec() {26   init {27      "isEnabledInternal should return false if the test is disabled in config" {28         val test = TestCase(29            name = TestName("foo"),30            descriptor = IsEnabledTest::class.toDescriptor().append("foo"),31            spec = this@IsEnabledTest,32            parent = null,33            test = {},34            config = ResolvedTestConfig.default.copy(enabled = { Enabled.disabled }),35            type = TestType.Test,36         )37         test.isEnabledInternal(ProjectConfiguration()).isEnabled shouldBe false38      }39      "isEnabledInternal should return false if it has an excluded tag" {40         val mytag = NamedTag("mytag")41         val ext = object : TagExtension {42            override fun tags(): TagExpression =43               TagExpression(emptySet(), setOf(mytag))44         }45         val c = ProjectConfiguration()46         c.registry.add(ext)47         val test = TestCase(48            name = TestName("foo"),49            descriptor = IsEnabledTest::class.toDescriptor().append("foo"),50            spec = this@IsEnabledTest,51            parent = null,52            test = {},53            config = ResolvedTestConfig.default.copy(tags = setOf(mytag)),54            type = TestType.Test,55         )56         test.isEnabledInternal(c).isEnabled shouldBe false57      }58      "isEnabledInternal should return false if it is excluded by a tag expression" {59         val mytag = NamedTag("mytag")60         val ext = object : TagExtension {61            override fun tags(): TagExpression = TagExpression("!mytag")62         }63         val c = ProjectConfiguration()64         c.registry.add(ext)65         val test = TestCase(66            name = TestName("foo"),67            descriptor = IsEnabledTest::class.toDescriptor().append("foo"),68            spec = this@IsEnabledTest,69            parent = null,70            test = {},71            config = ResolvedTestConfig.default.copy(tags = setOf(mytag)),72            type = TestType.Test,73         )74         test.isEnabledInternal(c).isEnabled shouldBe false75      }76      "isEnabledInternal should return false if it has no tags and included tags are set" {77         val yourtag = NamedTag("yourtag")78         val ext = object : TagExtension {79            override fun tags(): TagExpression = TagExpression(setOf(yourtag), emptySet())80         }81         val c = ProjectConfiguration()82         c.registry.add(ext)83         val mytag = NamedTag("mytag")84         val test = TestCase(85            name = TestName("foo"),86            descriptor = IsEnabledTest::class.toDescriptor().append("foo"),87            spec = this@IsEnabledTest,88            parent = null,89            test = {},90            config = ResolvedTestConfig.default.copy(tags = setOf(mytag)),91            type = TestType.Test,92         )93         test.isEnabledInternal(c).isEnabled shouldBe false94      }95      "isEnabledInternal should return false if it has no tags and a tag expression with include is set" {96         val ext = object : TagExtension {97            override fun tags(): TagExpression = TagExpression("yourtag")98         }99         val c = ProjectConfiguration()100         c.registry.add(ext)101         val mytag = NamedTag("mytag")102         val test = TestCase(103            name = TestName("foo"),104            descriptor = IsEnabledTest::class.toDescriptor().append("foo"),105            spec = this@IsEnabledTest,106            parent = null,107            test = {},108            config = ResolvedTestConfig.default.copy(tags = setOf(mytag)),109            type = TestType.Test,110         )111         test.isEnabledInternal(c).isEnabled shouldBe false112      }113      "isEnabledInternal should return false if the test name begins with a !" {114         val test = TestCase(115            name = TestName("!foo"),116            descriptor = IsEnabledTest::class.toDescriptor().append("!foo"),117            spec = this@IsEnabledTest,118            parent = null,119            test = {},120            config = ResolvedTestConfig.default,121            type = TestType.Test,122         )123         test.isEnabledInternal(ProjectConfiguration()).isEnabled shouldBe false124      }125      "isEnabledInternal should return false if the test is not focused and the spec contains OTHER focused tests" {126         val test = TestCase(127            name = TestName("foo"),128            descriptor = IsEnabledWithFocusTest::class.toDescriptor().append("foo"),129            spec = IsEnabledWithFocusTest(),130            parent = null,131            test = {},132            config = ResolvedTestConfig.default,133            type = TestType.Test,134         )135         test.isEnabledInternal(ProjectConfiguration()).isEnabled shouldBe false136      }137      "isEnabledInternal should return true if the test is focused and top level" {138         val test = TestCase(139            name = TestName("f:foo"),140            descriptor = IsEnabledWithFocusTest::class.toDescriptor().append("f:foo"),141            spec = IsEnabledWithFocusTest(),142            parent = null,143            test = {},144            config = ResolvedTestConfig.default,145            type = TestType.Test,146         )147         test.isEnabledInternal(ProjectConfiguration()).isEnabled shouldBe true148      }149      "isEnabledInternal should return true if not top level even if spec has top level focused tests" {150         val test = TestCase(151            name = TestName("f:my test"),152            descriptor = IsEnabledWithFocusTest::class.toDescriptor().append("f:my test").append("foo"),153            spec = IsEnabledWithFocusTest(),154            parent = null,155            test = {},156            config = ResolvedTestConfig.default,157            type = TestType.Test,158         )159         test.isEnabledInternal(ProjectConfiguration()).isEnabled shouldBe true160      }161      "isEnabledInternal should return false if a test filter excludes the test" {162         val filter = object : TestFilter {163            override fun filter(descriptor: Descriptor): TestFilterResult {164               return (descriptor.id.value == "f").toTestFilterResult(null)165            }166         }167         val c = ProjectConfiguration()168         c.registry.add(filter)169         TestCase(170            name = TestName("f"),171            descriptor = SomeTestClass::class.toDescriptor().append("f"),172            spec = SomeTestClass(),173            parent = null,174            test = {},175            config = ResolvedTestConfig.default,176            type = TestType.Test,177         ).isEnabledInternal(c).isEnabled shouldBe true178         TestCase(179            name = TestName("g"),180            descriptor = SomeTestClass::class.toDescriptor().append("g"),181            spec = SomeTestClass(),182            parent = null,183            test = {},184            config = ResolvedTestConfig.default,185            type = TestType.Test,186         ).isEnabledInternal(c).isEnabled shouldBe false187      }188      "isEnabled should use extensions when registered" {189         val ext = object : EnabledExtension {190            override suspend fun isEnabled(descriptor: Descriptor) =191               if (descriptor.id.value.contains("activateme"))192                  Enabled.enabled193               else194                  Enabled.disabled("descriptor name does not contain activateme")195         }196         val c = ProjectConfiguration()197         c.registry.add(ext)198         // this should be disabled because the extension says it is, even though it's normally enabled199         TestCase(200            name = TestName("enabled"),201            descriptor = SomeTestClass::class.toDescriptor().append("enabled"),202            spec = SomeTestClass(),203            parent = null,204            test = {},205            config = ResolvedTestConfig.default,206            type = TestType.Test,207         ).isEnabled(c).isEnabled shouldBe false208         TestCase(209            name = TestName("activateme"),210            descriptor = SomeTestClass::class.toDescriptor().append("activateme"),211            spec = SomeTestClass(),212            parent = null,213            test = {},214            config = ResolvedTestConfig.default,215            type = TestType.Test,216         ).isEnabled(c).isEnabled shouldBe true217      }218   }219}220class SomeTestClass : FunSpec({221   test("f") {}222   test("g") {}223})224class IsEnabledWithFocusTest : FunSpec({225   test("f: focused") {}226   test("not focused") {}227})...createSpecExecutorDelegate.kt
Source:createSpecExecutorDelegate.kt  
1package io.kotest.engine.spec2import io.kotest.common.ExperimentalKotest3import io.kotest.core.concurrency.CoroutineDispatcherFactory4import io.kotest.core.config.ProjectConfiguration5import io.kotest.core.spec.Spec6import io.kotest.core.test.TestCase7import io.kotest.core.test.TestResult8import io.kotest.engine.PromiseTestCaseExecutionListener9import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory10import io.kotest.engine.describe11import io.kotest.engine.it12import io.kotest.engine.listener.TestEngineListener13import io.kotest.engine.test.TestCaseExecutor14import io.kotest.engine.test.scopes.TerminalTestScope15import io.kotest.engine.test.interceptors.testNameEscape16import io.kotest.engine.test.names.getDisplayNameFormatter17import io.kotest.engine.test.status.isEnabledInternal18import io.kotest.engine.xit19import io.kotest.mpp.bestName20import kotlinx.coroutines.GlobalScope21import kotlinx.coroutines.promise22import kotlin.coroutines.coroutineContext23@ExperimentalKotest24internal actual fun createSpecExecutorDelegate(25   listener: TestEngineListener,26   defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,27   configuration: ProjectConfiguration,28): SpecExecutorDelegate = JavascriptSpecExecutorDelegate(configuration)29/**30 * Note: we need to use this: https://youtrack.jetbrains.com/issue/KT-2222831 */32@ExperimentalKotest33internal class JavascriptSpecExecutorDelegate(private val configuration: ProjectConfiguration) : SpecExecutorDelegate {34   private val formatter = getDisplayNameFormatter(35      configuration.registry,36      configuration37   )38   private val materializer = Materializer(configuration)39   override suspend fun execute(spec: Spec): Map<TestCase, TestResult> {40      val cc = coroutineContext41      // we use the spec itself as an outer/parent test.42      describe(testNameEscape(spec::class.bestName())) {43         materializer.materialize(spec).forEach { root ->44            val testDisplayName = testNameEscape(formatter.format(root))45            // todo find a way to delegate this to the test case executor46            val enabled = root.isEnabledInternal(configuration)47            if (enabled.isEnabled) {48               // we have to always invoke `it` to start the test so that the js test framework doesn't exit49               // before we invoke our callback. This also gives us the handle to the done callback.50               val test = it(testDisplayName) { done ->51                  // ideally we'd just launch the executor and have the listener setup the test52                  // but we can't launch a promise inside the describe and have it resolve the "it"53                  // this means we must duplicate the isEnabled check outside of the executor54                  GlobalScope.promise {55                     TestCaseExecutor(56                        PromiseTestCaseExecutionListener(done),57                        NoopCoroutineDispatcherFactory,58                        configuration59                     ).execute(root, TerminalTestScope(root, cc))60                  }61                  // we don't want to return the promise as the js frameworks will use that for test resolution62                  // instead of the done callback, and we prefer the callback as it allows for custom timeouts63                  Unit64               }65               // some frameworks default to a 2000 timeout,66               // here we set to a high number and use the timeout support kotest provides via coroutines67               test.timeout(Int.MAX_VALUE)68               Unit69            } else {70               xit(testDisplayName) {}71            }72         }73      }74      return emptyMap()75   }76}...enabled.kt
Source:enabled.kt  
1package io.kotest.engine.test.status2import io.kotest.core.config.ProjectConfiguration3import io.kotest.core.extensions.EnabledExtension4import io.kotest.core.test.Enabled5import io.kotest.core.test.TestCase6import io.kotest.engine.spec.SpecExtensions7import io.kotest.engine.tags.runtimeTags8/**9 * Returns [Enabled.enabled] if the given [TestCase] is enabled based on default rules10 * from [isEnabledInternal] or any registered [EnabledExtension]s.11 */12suspend fun TestCase.isEnabled(conf: ProjectConfiguration): Enabled {13   val internal = isEnabledInternal(conf)14   return if (!internal.isEnabled) {15      internal16   } else {17      val disabled = SpecExtensions(conf.registry)18         .extensions(spec)19         .filterIsInstance<EnabledExtension>()20         .map { it.isEnabled(descriptor) }21         .firstOrNull { it.isDisabled }22      disabled ?: Enabled.enabled23   }24}25/**26 * Determines enabled status by using [TestEnabledExtension]s.27 */28internal fun TestCase.isEnabledInternal(conf: ProjectConfiguration): Enabled {29   val extensions = listOf(30      TestConfigEnabledExtension,31      TagsEnabledExtension(conf.runtimeTags()),32      TestFilterEnabledExtension(conf.registry),33      SystemPropertyTestFilterEnabledExtension,34      FocusEnabledExtension,35      BangTestEnabledExtension,36      SeverityLevelEnabledExtension,37   )38   return extensions.fold(Enabled.enabled) { acc, ext -> if (acc.isEnabled) ext.isEnabled(this) else acc }39}...TestCase.isEnabledInternal
Using AI Code Generation
1    fun `test to check isEnabledInternal method of io.kotest.engine.test.status.enabled class`() {2        val testCase = TestCase(3            spec = object : FunSpec() {},4            name = TestCaseName("test"),5            description = Description.spec("spec"),6            config = TestCaseConfig(),7            test = {},TestCase.isEnabledInternal
Using AI Code Generation
1val enabled = TestCase.isEnabledInternal(testCase)2if (enabled) {3println("Test case)isenabled")4}else{5println("Test case is disabled")6}7}8}9}10        val actual = enabled.isEnabledInternal(testCase)11        assertThat(actual).isTrue()12    }13    fun `test to check isEnabledInternal method of io.kotest.engine.test.status.enabled class`() {14        val testCase = TestCase(15            spec = object : FunSpec() {},16            name = TestCaseName("test"),17            description = Description.spec("spec"),TestCase.isEnabledInternal
Using AI Code Generation
1            config = TestCaseConfig(),2            test = {},3        val actual = enabled.isEnabledInternal(testCase)4        assertThat(actual).isFalse()5    }6    fun `test to check isEnabledInternal method of io.kotest.engine.test.status.enabled class`() {7        val testCase = TestCase(8            spec = object : FunSpec() {},9            name = TestCaseName("test"),10            description = Description.spec("spec"),11            config = TestCaseConfig(),12            test = {},13        val actual = enabled.isEnabledInternal(testCase)14        assertThat(actual).isFalse()15    }16    fun `test to check isEnabledInternal method of io.kotest.engine.test.status.enabled class`() {17        val testCase = TestCase(18            spec = object : FunSpec() {},19            name = TestCaseName("test"),20            description = Description.spec("spec"),TestCase.isEnabledInternal
Using AI Code Generation
1val enabled = TestCase.isEnabledInternal(testCase)2if (enabled) {3println("Test case is enabled")4} else {5println("Test case is disabled")6}7}8}9}TestCase.isEnabledInternal
Using AI Code Generation
1val testCaseConfig = TestCaseConfig() val testCaseConfig = TestCaseConfig()2testCaseConfig.isEnabled() testCaseConfig.isEnabled()3import io.kotest.core.test.TestCaseConfig import io.kotest.core.test.TestCaseConfig4import io.kotest.core.test.isEnabled import io.kotest.core.test.isEnabled5testCaseConfig.isEnabled() testCaseConfig.isEnabled()6import io.kotest.core.test.TestCaseConfig.isEnabled import io.kotest.core.test.TestCaseConfig.isEnabledLearn 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!!
