Best Kotest code snippet using io.kotest.engine.tags.active
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.FunSpec...
TagsTest.kt
Source:TagsTest.kt
1package com.sksamuel.kotest.engine.tags2import io.kotest.core.NamedTag3import io.kotest.core.Tag4import io.kotest.core.TagExpression5import io.kotest.core.spec.style.StringSpec6import io.kotest.engine.tags.isActive7import io.kotest.engine.tags.parse8import io.kotest.matchers.shouldBe9class TagsTest : StringSpec() {10 object Moo : Tag()11 object Foo : Tag()12 object Roo : Tag()13 init {14 "test with include and exclude tags" {15 val tags = TagExpression(setOf(Foo, NamedTag("boo")), setOf(Moo))16 tags.parse().isActive(Foo) shouldBe true17 tags.parse().isActive(NamedTag("boo")) shouldBe true18 tags.parse().isActive(Moo) shouldBe false // moo excluded19 tags.parse().isActive(NamedTag("goo")) shouldBe false // missing any of the included20 tags.parse().isActive(Roo) shouldBe false // missing any of the included21 tags.parse().isActive(setOf(Moo, Roo)) shouldBe false // moo excluded22 tags.parse().isActive(setOf(Moo, Foo)) shouldBe false // moo excluded23 tags.parse().isActive(setOf(NamedTag("boo"), Foo)) shouldBe true // has both the included24 }25 "test with include tags" {26 val tags = TagExpression(setOf(Foo, NamedTag("boo")), emptySet())27 tags.parse().isActive(Foo) shouldBe true28 tags.parse().isActive(NamedTag("boo")) shouldBe true29 tags.parse().isActive(Moo) shouldBe false30 tags.parse().isActive(NamedTag("goo")) shouldBe false31 tags.parse().isActive(Roo) shouldBe false32 }33 "test with exclude tags" {34 val tags = TagExpression(emptySet(), setOf(Moo))35 tags.parse().isActive(Foo) shouldBe true36 tags.parse().isActive(NamedTag("boo")) shouldBe true37 tags.parse().isActive(Moo) shouldBe false38 tags.parse().isActive(NamedTag("goo")) shouldBe true39 tags.parse().isActive(Roo) shouldBe true40 }41 "test with no tags" {42 val tags = TagExpression(emptySet(), emptySet())43 tags.parse().isActive(Foo) shouldBe true44 tags.parse().isActive(NamedTag("boo")) shouldBe true45 tags.parse().isActive(Moo) shouldBe true46 tags.parse().isActive(NamedTag("goo")) shouldBe true47 tags.parse().isActive(Roo) shouldBe true48 }49 "test with simple expression" {50 val tags = TagExpression("Foo")51 tags.parse().isActive(Foo) shouldBe true52 tags.parse().isActive(Roo) shouldBe false53 tags.parse().isActive(Moo) shouldBe false54 tags.parse().isActive(setOf(Foo, Roo)) shouldBe true55 tags.parse().isActive(setOf(Foo, Moo, Roo)) shouldBe true56 }57 "test with or expression" {58 val tags = TagExpression("Foo | Roo")59 tags.parse().isActive(Foo) shouldBe true60 tags.parse().isActive(Roo) shouldBe true61 tags.parse().isActive(Moo) shouldBe false62 tags.parse().isActive(setOf(Foo, Roo)) shouldBe true63 tags.parse().isActive(setOf(Foo, Moo, Roo)) shouldBe true64 }65 "test with and expression" {66 val tags = TagExpression("Foo & Roo")67 tags.parse().isActive(Foo) shouldBe false68 tags.parse().isActive(Roo) shouldBe false69 tags.parse().isActive(Moo) shouldBe false70 tags.parse().isActive(setOf(Foo, Roo)) shouldBe true71 tags.parse().isActive(setOf(Foo, Moo, Roo)) shouldBe true72 tags.parse().isActive(setOf(Foo, Moo)) shouldBe false73 }74 "test with not expression" {75 val tags = TagExpression("!Roo")76 tags.parse().isActive(Foo) shouldBe true77 tags.parse().isActive(Roo) shouldBe false78 tags.parse().isActive(Moo) shouldBe true79 tags.parse().isActive(setOf(Foo, Roo)) shouldBe false80 tags.parse().isActive(setOf(Foo, Moo, Roo)) shouldBe false81 tags.parse().isActive(setOf(Foo, Moo)) shouldBe true82 }83 "test with not expression and join" {84 val tags = TagExpression("!Roo & Foo")85 tags.parse().isActive(Foo) shouldBe true86 tags.parse().isActive(Roo) shouldBe false // roo excluded87 tags.parse().isActive(Moo) shouldBe false // missing foo88 tags.parse().isActive(setOf(Foo, Roo)) shouldBe false89 tags.parse().isActive(setOf(Foo, Moo, Roo)) shouldBe false90 tags.parse().isActive(setOf(Foo, Moo)) shouldBe true91 tags.parse().isActive(setOf(Moo, Roo)) shouldBe false92 }93 "test with parens" {94 val tags = TagExpression("(Roo | Foo) & Moo")95 tags.parse().isActive(Foo) shouldBe false // missing Moo96 tags.parse().isActive(Roo) shouldBe false // missing Moo97 tags.parse().isActive(Moo) shouldBe false // missing Roo | Foo98 tags.parse().isActive(setOf(Roo, Moo)) shouldBe true99 tags.parse().isActive(setOf(Foo, Moo)) shouldBe true // foo is excluded100 tags.parse().isActive(setOf(Foo, Moo, Roo)) shouldBe true101 }102 "test with not on parens" {103 val tags = TagExpression("!(Roo | Foo) & Moo")104 tags.parse().isActive(Foo) shouldBe false // Foo excluded105 tags.parse().isActive(Roo) shouldBe false // Roo excluded106 tags.parse().isActive(Moo) shouldBe true107 tags.parse().isActive(setOf(Roo, Moo)) shouldBe false108 tags.parse().isActive(setOf(Foo, Moo)) shouldBe false109 tags.parse().isActive(setOf(Foo, Moo, Roo)) shouldBe false110 }111 }112}...
TestEngine.kt
Source:TestEngine.kt
1package io.kotest.engine2import io.kotest.common.ExperimentalKotest3import io.kotest.common.KotestInternal4import io.kotest.common.Platform5import io.kotest.common.platform6import io.kotest.core.TagExpression7import io.kotest.core.config.ProjectConfiguration8import io.kotest.core.project.TestSuite9import io.kotest.engine.interceptors.EngineContext10import io.kotest.engine.interceptors.EngineInterceptor11import io.kotest.engine.listener.TestEngineListener12import io.kotest.engine.tags.runtimeTags13import io.kotest.mpp.Logger14data class EngineResult(val errors: List<Throwable>) {15 companion object {16 val empty = EngineResult(emptyList())17 }18 fun addError(t: Throwable): EngineResult {19 return EngineResult(errors + t)20 }21}22@KotestInternal23data class TestEngineConfig(24 val listener: TestEngineListener,25 val interceptors: List<EngineInterceptor>,26 val configuration: ProjectConfiguration,27 val explicitTags: TagExpression?,28)29/**30 * Multiplatform Kotest Test Engine.31 */32@KotestInternal33class TestEngine(private val config: TestEngineConfig) {34 private val logger = Logger(this::class)35 /**36 * Starts execution of the given [TestSuite], intercepting calls via [EngineInterceptor]s.37 *38 * It is recommended that this method is not invoked, but instead the engine39 * is launched via the [TestEngineLauncher].40 */41 @OptIn(KotestInternal::class, ExperimentalKotest::class)42 internal suspend fun execute(suite: TestSuite): EngineResult {43 logger.log { Pair(null, "Executing test suite with ${suite.specs.size} specs") }44 val innerExecute: suspend (EngineContext) -> EngineResult = { context ->45 val scheduler = when (platform) {46 Platform.JVM -> ConcurrentTestSuiteScheduler(47 config.configuration.concurrentSpecs ?: config.configuration.parallelism,48 context,49 )50 Platform.JS -> SequentialTestSuiteScheduler(context)51 Platform.Native -> SequentialTestSuiteScheduler(context)52 }53 scheduler.schedule(context.suite)54 }55 logger.log { Pair(null, "${config.interceptors.size} engine interceptors") }56 val execute = config.interceptors.foldRight(innerExecute) { extension, next ->57 { context -> extension.intercept(context, next) }58 }59 val tags = config.configuration.runtimeTags()60 logger.log { Pair(null, "TestEngine: Active tags: ${tags.expression}") }61 return execute(EngineContext(suite, config.listener, tags, config.configuration))62 }63}...
active.kt
Source:active.kt
2import io.kotest.core.Tag3import io.kotest.core.spec.Spec4import kotlin.reflect.KClass5/**6 * Returns true if the given [Spec] class could contain an active test based on further tags.7 * Returns false if the spec has been explicitly excluded and should not be instantiated.8 */9fun Expression?.isPotentiallyActive(kclass: KClass<out Spec>): Boolean {10 // nothing is excluded if the expression is null11 if (this == null) return true12 // if the class is not tagged then it is not excluded13 val tags = kclass.tags()14 if (tags.isEmpty()) return true15 return isPotentiallyActive(tags.toSet()) ?: true16}17internal fun Expression.isPotentiallyActive(tags: Set<Tag>): Boolean? {18 return when (this) {19 is Expression.Or -> left.isPotentiallyActive(tags) ?: true || right.isPotentiallyActive(tags) ?: true20 is Expression.And -> left.isPotentiallyActive(tags) ?: true && right.isPotentiallyActive(tags) ?: true21 is Expression.Not -> expr.isPotentiallyActive(tags)?.not()22 is Expression.Identifier -> if (tags.map { it.name }.contains(ident)) true else null23 }24}25/**26 * Returns true if the the given tag should be considered active based27 * on the current tag expression.28 */29fun Expression?.isActive(tag: Tag): Boolean = isActive(setOf(tag))30/**31 * Returns true if the the given set of tags should be considered active based32 * on the current tag expression.33 */34fun Expression?.isActive(tags: Set<Tag>): Boolean {35 // everything is always active when no tag expression is provided36 if (this == null) return true37 return evaluate(tags, this)38}39private fun evaluate(tags: Set<Tag>, expression: Expression): Boolean {40 return when (expression) {41 is Expression.Or -> evaluate(tags, expression.left) || evaluate(tags, expression.right)42 is Expression.And -> evaluate(tags, expression.left) && evaluate(tags, expression.right)43 is Expression.Not -> !evaluate(tags, expression.expr)44 is Expression.Identifier -> tags.map { it.name }.contains(expression.ident)45 }46}...
RequiresTagSpecInterceptor.kt
Source:RequiresTagSpecInterceptor.kt
1package io.kotest.engine.spec.interceptor2import io.kotest.common.flatMap3import io.kotest.core.NamedTag4import io.kotest.core.annotation.RequiresTag5import io.kotest.core.annotation.requirestag.wrapper6import io.kotest.core.config.ProjectConfiguration7import io.kotest.core.config.ExtensionRegistry8import io.kotest.core.filter.SpecFilter9import io.kotest.core.spec.SpecRef10import io.kotest.core.test.TestCase11import io.kotest.core.test.TestResult12import io.kotest.engine.listener.TestEngineListener13import io.kotest.engine.spec.SpecExtensions14import io.kotest.engine.tags.isActive15import io.kotest.engine.tags.parse16import io.kotest.engine.tags.runtimeTags17import io.kotest.mpp.annotation18/**19 * A [SpecFilter] which will ignore specs if they are annotated with @[RequiresTag]20 * and those tags are not present in the runtime tags.21 */22internal class RequiresTagSpecInterceptor(23 private val listener: TestEngineListener,24 private val configuration: ProjectConfiguration,25 private val registry: ExtensionRegistry,26) : SpecRefInterceptor {27 override suspend fun intercept(28 ref: SpecRef,29 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>30 ): Result<Map<TestCase, TestResult>> {31 return when (val annotation = ref.kclass.annotation<RequiresTag>()) {32 null -> fn(ref)33 else -> {34 val requiredTags = annotation.wrapper.map { NamedTag(it) }.toSet()35 val expr = configuration.runtimeTags().parse()36 if (requiredTags.isEmpty() || expr.isActive(requiredTags)) {37 fn(ref)38 } else {39 runCatching { listener.specIgnored(ref.kclass, "Disabled by @RequiresTag") }40 .flatMap { SpecExtensions(registry).ignored(ref.kclass, "Disabled by @RequiresTag") }41 .map { emptyMap() }42 }43 }44 }45 }46}...
TagsExcludedDiscoveryExtension.kt
Source:TagsExcludedDiscoveryExtension.kt
1package io.kotest.engine.spec.interceptor2import io.kotest.common.flatMap3import io.kotest.core.TagExpression4import io.kotest.core.config.ProjectConfiguration5import io.kotest.core.spec.Spec6import io.kotest.core.spec.SpecRef7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestResult9import io.kotest.engine.listener.TestEngineListener10import io.kotest.engine.spec.SpecExtensions11import io.kotest.engine.tags.isPotentiallyActive12import io.kotest.engine.tags.parse13import io.kotest.engine.tags.runtimeTags14/**15 * Filters any [Spec] that can be eagerly excluded based on the @[TagExpression] annotation at the class level.16 */17class TagsExcludedSpecInterceptor(18 private val listener: TestEngineListener,19 private val conf: ProjectConfiguration,20) : SpecRefInterceptor {21 private val extensions = SpecExtensions(conf.registry)22 override suspend fun intercept(23 ref: SpecRef,24 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>25 ): Result<Map<TestCase, TestResult>> {26 val potentiallyActive = conf.runtimeTags().parse().isPotentiallyActive(ref.kclass)27 return if (potentiallyActive) {28 fn(ref)29 } else {30 runCatching { listener.specIgnored(ref.kclass, null) }31 .flatMap { extensions.ignored(ref.kclass, "Skipped by tags") }32 .map { emptyMap() }33 }34 }35}...
TagsEnabledExtension.kt
Source:TagsEnabledExtension.kt
1package io.kotest.engine.test.status2import io.kotest.core.TagExpression3import io.kotest.core.test.Enabled4import io.kotest.core.test.TestCase5import io.kotest.engine.tags.isActive6import io.kotest.engine.tags.parse7import io.kotest.mpp.log8/**9 * A [TestEnabledExtension] that uses [io.kotest.core.Tag]s.10 *11 * This extension disables a test if:12 *13 * - Excluded tags have been specified and this test has a [io.kotest.core.Tag] which is one of those excluded.14 * - Included tags have been specified and this test either has no tags,15 * or does not have any of the specified inclusion tags.16 *17 * Note: tags are attached to tests either through test config, or at the spec level.18 */19internal class TagsEnabledExtension(private val tags: TagExpression) : TestEnabledExtension {20 override fun isEnabled(testCase: TestCase): Enabled {21 val enabledInTags = tags.parse().isActive(testCase.config.tags)22 if (!enabledInTags) {23 return Enabled24 .disabled("Disabled by tags: ${tags.expression}")25 .also { it.reason?.let { log { it } } }26 }27 return Enabled.enabled28 }29}...
runtime.kt
Source:runtime.kt
...3import io.kotest.core.TagExpression4import io.kotest.core.config.ProjectConfiguration5import io.kotest.core.extensions.TagExtension6/**7 * Returns runtime active [Tag]'s by invoking all registered [TagExtension]s and combining8 * any returned tags into a [TagExpression] container.9 */10fun ProjectConfiguration.runtimeTags(): TagExpression {11 val extensions = this.registry.all().filterIsInstance<TagExtension>()12 return if (extensions.isEmpty()) TagExpression.Empty else extensions.map { it.tags() }.reduce { a, b -> a.combine(b) }13}...
active
Using AI Code Generation
1+ if (activeTags.contains(tag.name)) {2+ }3+ }4+ override fun filter(spec: Spec): Spec? {5+ if (activeTags.contains(spec::class.simpleName)) {6+ }7+ }8+ override fun filter(testCase: TestCase): TestCase? {9+ if (activeTags.contains(testCase.spec::class.simpleName)) {10+ }11+ }12+ override fun filter(testCase: TestCase, result: TestResult): TestResult? {13+ if (activeTags.contains(testCase.spec::class.simpleName)) {14+ }15+ }16+}17+class TagFilterListener : TestEngineListener {18+ override suspend fun engineStarted(classes: List<KClass<out Spec>>) {
active
Using AI Code Generation
1+@ActiveTag("activeTag")2 class ActiveTagTest : FunSpec({3 test("this test is active") {4@@ -55,6 +60,10 @@ class ActiveTagTest : FunSpec({5 test("this test is not active") {6 fail("this test should not run")7+ }8+ test("this test is active") {9+ fail("this test should run")10 }11 })12-import io.kotest.core.spec.style.FunSpec13+import io.kotest.core.spec.style.FunSpec14 import io.kotest.engine.tags.ActiveTag15 import io.kotest.matchers.shouldBe16@@ -8,6 +8,7 @@ import io.kotest.matchers.shouldBe17 @ActiveTag("activeTag")18 class ActiveTagTest : FunSpec({19 test("this test is active") {20 fail("this test should not run")21 }22@@ -15,6 +16,7 @@ class ActiveTagTest : FunSpec({23 test("this test is not active") {24 fail("this test should not run")25+ }26 test("this test is active") {27 fail("this test should run")
active
Using AI Code Generation
1+ val activeTagRegex = Regex(".*$activeTag.*")2+ val activeTagReplacementRegex = Regex(".*$activeTagReplacement.*")3+ val activeIfTagRegex = Regex(".*$activeIfTag.*")4+ val activeIfTagReplacementRegex = Regex(".*$activeIfTagReplacement.*")5+ val inactiveTagRegex = Regex(".*$inactiveTag.*")6+ val inactiveTagReplacementRegex = Regex(".*$inactiveTagReplacement.*")7+ val inactiveIfTagRegex = Regex(".*$inactiveIfTag.*")8+ val inactiveIfTagReplacementRegex = Regex(".*$inactiveIfTagReplacement.*")9+ val tagsRegex = Regex(".*$tags.*")10+ val tagsReplacementRegex = Regex(".*$tagsReplacement.*")11+ val activeTagRegexes = listOf(
active
Using AI Code Generation
1+@Tag("active")2 class ActiveTest : FunSpec({3 test("active test") {4 }5 })6class MySpec : FunSpec({7})8class MySpec : FunSpec({9})
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!!