How to use tags method of io.kotest.core.extensions.TagExtension class

Best Kotest code snippet using io.kotest.core.extensions.TagExtension.tags

TagsAnnotationInheritenceTest.kt

Source:TagsAnnotationInheritenceTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.tags2import io.kotest.core.Tag3import io.kotest.core.TagExpression4import io.kotest.core.config.ProjectConfiguration5import io.kotest.core.extensions.TagExtension6import io.kotest.core.annotation.Isolate7import io.kotest.core.spec.style.FunSpec8import io.kotest.core.test.TestCaseOrder9import io.kotest.engine.spec.Materializer10import io.kotest.engine.test.status.isEnabledInternal11import io.kotest.matchers.shouldBe12@Isolate13class TagsAnnotationInheritenceTest : FunSpec() {14 init {15 test("simple tag") {16 val ext = object : TagExtension {17 override fun tags(): TagExpression = TagExpression.include(Linux)18 }19 val conf = ProjectConfiguration()20 conf.registry.add(ext)21 conf.testCaseOrder = TestCaseOrder.Random22 Materializer(conf).materialize(MyTestClass())23 .filter { it.isEnabledInternal(conf).isEnabled }24 .map { it.name.testName }25 .toSet() shouldBe setOf("a", "b", "c", "d")26 }27 test("simple exclude tag") {28 val ext = object : TagExtension {29 override fun tags(): TagExpression = TagExpression.exclude(Linux)30 }31 val conf = ProjectConfiguration()32 conf.registry.add(ext)33 conf.testCaseOrder = TestCaseOrder.Random34 // all tests should be filtered out because of the @Tags35 Materializer(conf).materialize(MyTestClass())36 .filter { it.isEnabledInternal(conf).isEnabled }37 .map { it.name.testName }38 .toSet() shouldBe emptySet()39 }40 test("inheritence with OR") {41 val ext = object : TagExtension {42 override fun tags(): TagExpression = TagExpression("Linux | Mysql")43 }44 val conf = ProjectConfiguration()45 conf.registry.add(ext)46 conf.testCaseOrder = TestCaseOrder.Random47 // linux is included for all, and we're using an 'or'48 Materializer(conf).materialize(MyTestClass())49 .filter { it.isEnabledInternal(conf).isEnabled }50 .map { it.name.testName }51 .toSet() shouldBe setOf("a", "b", "c", "d")52 }53 test("inheritence with AND") {54 val ext = object : TagExtension {55 override fun tags(): TagExpression = TagExpression.include(Linux).exclude(Postgres)56 }57 val conf = ProjectConfiguration()58 conf.registry.add(ext)59 conf.testCaseOrder = TestCaseOrder.Random60 // linux should be included for all, but then postgres tests excluded as well61 Materializer(conf).materialize(MyTestClass())62 .filter { it.isEnabledInternal(conf).isEnabled }63 .map { it.name.testName }64 .toSet() shouldBe setOf("a", "d")65 }66 test("@Tags should be ignored when not applicable to an exclude") {67 val ext = object : TagExtension {68 override fun tags(): TagExpression = TagExpression.exclude(Mysql)69 }70 val conf = ProjectConfiguration()71 conf.registry.add(ext)72 conf.testCaseOrder = TestCaseOrder.Random73 // Mysql tests should be excluded74 Materializer(conf).materialize(MyTestClass())75 .filter { it.isEnabledInternal(conf).isEnabled }76 .map { it.name.testName }77 .toSet() shouldBe setOf("b", "d")78 }79 test("@Tags should be ignored when not applicable to an test") {80 val ext = object : TagExtension {81 override fun tags(): TagExpression = TagExpression.include(Postgres)82 }83 val conf = ProjectConfiguration()84 conf.registry.add(ext)85 conf.testCaseOrder = TestCaseOrder.Random86 // Mysql tests should be excluded87 Materializer(conf).materialize(MyTestClass())88 .filter { it.isEnabledInternal(conf).isEnabled }89 .map { it.name.testName }90 .toSet() shouldBe setOf("b", "c")91 }92 }93}94object Linux : Tag()95object UnitTest : Tag()96object Mysql : Tag()97object Postgres : Tag()98@io.kotest.core.annotation.Tags("Linux")99private class MyTestClass : FunSpec() {100 init {101 tags(UnitTest)102 test("a").config(tags = setOf(Mysql)) { }103 test("b").config(tags = setOf(Postgres)) { }104 test("c").config(tags = setOf(Postgres, Mysql)) { }105 test("d") { }106 }107}...

Full Screen

Full Screen

ExcludeTagExtensionTest.kt

Source:ExcludeTagExtensionTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.tags2import io.kotest.assertions.fail3import io.kotest.core.Tag4import io.kotest.core.TagExpression5import io.kotest.core.extensions.TagExtension6import io.kotest.core.annotation.Isolate7import io.kotest.core.spec.style.FunSpec8import io.kotest.core.spec.style.StringSpec9import io.kotest.core.test.TestCase10import io.kotest.engine.TestEngineLauncher11import io.kotest.engine.listener.AbstractTestEngineListener12object Exclude : Tag()13private object ExcludeTagExtension : TagExtension {14 override fun tags(): TagExpression = TagExpression.exclude(Exclude)15}16@Isolate17class ExcludeTagExtensionTest : FunSpec() {18 init {19 test("tag extensions should be applied to tests with tag inherited from spec") {20 val listener = object : AbstractTestEngineListener() {21 override suspend fun testStarted(testCase: TestCase) {22 fail(testCase.name.testName + " should not run")23 }24 }25 val conf = io.kotest.core.config.ProjectConfiguration()26 conf.registry.add(ExcludeTagExtension)27 TestEngineLauncher(listener)28 .withClasses(ExcludedSpec::class)29 .withConfiguration(conf)30 .launch()31 }32 }33}34private class ExcludedSpec : StringSpec({35 tags(Exclude)36 "should not run" {37 fail("Shouldn't get here")38 }39})...

Full Screen

Full Screen

SystemPropertyTagExtension.kt

Source:SystemPropertyTagExtension.kt Github

copy

Full Screen

...5import io.kotest.core.extensions.TagExtension6import io.kotest.core.internal.KotestEngineProperties7import io.kotest.mpp.syspropOrEnv8/**9 * This [TagExtension] includes and excludes tags using the system properties:10 * [KotestEngineProperties.tagExpression], [KotestEngineProperties.includeTags]11 * and [KotestEngineProperties.excludeTags].12 *13 * Note: If [KotestEngineProperties.tagExpression] is used then the other two properties will be ignored.14 *15 * On non-JVM targets this extension will have no effect.16 */17object SystemPropertyTagExtension : TagExtension {18 override fun tags(): TagExpression {19 fun readTagsProperty(name: String): List<Tag> =20 (syspropOrEnv(name) ?: "").split(',').filter { it.isNotBlank() }.map { NamedTag(it.trim()) }21 val includedTags = readTagsProperty(KotestEngineProperties.includeTags)22 val excludedTags = readTagsProperty(KotestEngineProperties.excludeTags)23 val expression = syspropOrEnv(KotestEngineProperties.tagExpression)24 return if (expression == null) TagExpression(includedTags.toSet(), excludedTags.toSet()) else TagExpression(expression)25 }26}...

Full Screen

Full Screen

ProjectConfig.kt

Source:ProjectConfig.kt Github

copy

Full Screen

...13import java.nio.file.Path14object Windows: Tag()15object NotWindows: Tag()16object SystemTagExtension: TagExtension {17 override fun tags(): TagExpression {18 return if(isWindows()) {19 TagExpression.exclude(NotWindows)20 } else {21 TagExpression.exclude(Windows)22 }23 }24 private fun isWindows(): Boolean {25 return System.getProperty("os.name")26 .lowercase()27 .contains("windows")28 }29}30/**31 * kotest config...

Full Screen

Full Screen

UI.kt

Source:UI.kt Github

copy

Full Screen

...7import java.awt.GraphicsDevice8import java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment9object UI : Tag()10object UITagExtension : TagExtension {11 override fun tags(): TagExpression =12 if (shouldRunUITests()) Empty else exclude(UI)13 private fun shouldRunUITests() = displayIsVirtual() || !runningAsPartOfBuild()14 // TODO somehow recognise this without hard coding a DISPLAY number15 // If you change this, change scripts/simple-xvfb-run.sh16 private fun displayIsVirtual(): Boolean =17 getLocalGraphicsEnvironment().screenDevices.any { it.isX11() } &&18 System.getenv("DISPLAY") == ":99"19 private fun GraphicsDevice.isX11() = this::class.java.simpleName.startsWith("X11")20 private fun runningAsPartOfBuild() = System.getenv("BUILD_SYSTEM") != null21}...

Full Screen

Full Screen

runtime.kt

Source:runtime.kt Github

copy

Full Screen

1package io.kotest.engine.tags2import io.kotest.core.Tag3import 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}...

Full Screen

Full Screen

Docker.kt

Source:Docker.kt Github

copy

Full Screen

...5import io.kotest.core.TagExpression.Companion.exclude6import io.kotest.core.extensions.TagExtension7object Docker : Tag()8object DockerTagExtension : TagExtension {9 override fun tags(): TagExpression =10 if (shouldRunDockerTests()) Empty else exclude(Docker)11 private fun shouldRunDockerTests() = !runningAsPartOfBuild()12 private fun runningAsPartOfBuild() = System.getenv("BUILD_SYSTEM") != null13}...

Full Screen

Full Screen

SpecifiedTagsTagExtension.kt

Source:SpecifiedTagsTagExtension.kt Github

copy

Full Screen

...3import io.kotest.core.extensions.TagExtension4/**5 * A [TagExtension] that will provide the [TagExpression] given to the constructor.6 */7class SpecifiedTagsTagExtension(private val tags: TagExpression) : TagExtension {8 override fun tags(): TagExpression = tags9}...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in TagExtension

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful