Best Kotest code snippet using io.kotest.engine.test.status.SystemPropertyTestFilterEnabledExtension.filter
SystemPropertyTestFilterEnabledExtensionTest.kt
Source:SystemPropertyTestFilterEnabledExtensionTest.kt
...13import io.kotest.extensions.system.withSystemProperty14import io.kotest.matchers.shouldBe15class SystemPropertyTestFilterEnabledExtensionTest : FunSpec() {16 init {17 test("should include tests when no filter system property or environment variable is specified") {18 val tc = TestCase(19 SystemPropertyTestFilterEnabledExtensionTest::class.toDescriptor().append("foo"),20 TestName("foo"),21 SystemPropertyTestFilterEnabledExtensionTest(),22 {},23 sourceRef(),24 TestType.Test25 )26 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)27 }28 test("should include tests that match pattern in system property") {29 val tc = TestCase(30 SystemPropertyTestFilterEnabledExtensionTest::class.toDescriptor().append("foo"),31 TestName("foo"),32 SystemPropertyTestFilterEnabledExtensionTest(),33 {},34 sourceRef(),35 TestType.Test36 )37 withSystemProperty(KotestEngineProperties.filterTests, "foo") {38 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)39 }40 withSystemProperty(KotestEngineProperties.filterTests, "f*") {41 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)42 }43 withSystemProperty(KotestEngineProperties.filterTests, "*o") {44 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)45 }46 }47 test("should exclude tests that do not match pattern in system property") {48 val tc = TestCase(49 SystemPropertyTestFilterEnabledExtensionTest::class.toDescriptor().append("foo"),50 TestName("foo"),51 SystemPropertyTestFilterEnabledExtensionTest(),52 {},53 sourceRef(),54 TestType.Test55 )56 withSystemProperty(KotestEngineProperties.filterTests, "goo") {57 SystemPropertyTestFilterEnabledExtension.isEnabled(tc)58 .shouldBe(Enabled.disabled("Excluded by kotest.filter.tests test filter: goo"))59 }60 withSystemProperty(KotestEngineProperties.filterTests, "g*") {61 SystemPropertyTestFilterEnabledExtension.isEnabled(tc)62 .shouldBe(Enabled.disabled("Excluded by kotest.filter.tests test filter: g.*?"))63 }64 withSystemProperty(KotestEngineProperties.filterTests, "*p") {65 SystemPropertyTestFilterEnabledExtension.isEnabled(tc)66 .shouldBe(Enabled.disabled("Excluded by kotest.filter.tests test filter: .*?p"))67 }68 }69 test("should include tests that match pattern in environment variable") {70 val tc = TestCase(71 SystemPropertyTestFilterEnabledExtensionTest::class.toDescriptor().append("foo"),72 TestName("foo"),73 SystemPropertyTestFilterEnabledExtensionTest(),74 {},75 sourceRef(),76 TestType.Test77 )78 withEnvironment(KotestEngineProperties.filterTests, "foo") {79 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)80 }81 withEnvironment(KotestEngineProperties.filterTests, "f*") {82 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)83 }84 withEnvironment(KotestEngineProperties.filterTests, "*o") {85 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)86 }87 }88 test("should exclude tests that do not match pattern in environment variable") {89 val tc = TestCase(90 SystemPropertyTestFilterEnabledExtensionTest::class.toDescriptor().append("foo"),91 TestName("foo"),92 SystemPropertyTestFilterEnabledExtensionTest(),93 {},94 sourceRef(),95 TestType.Test96 )97 withEnvironment(KotestEngineProperties.filterTests, "goo") {98 SystemPropertyTestFilterEnabledExtension.isEnabled(tc)99 .shouldBe(Enabled.disabled("Excluded by kotest.filter.tests test filter: goo"))100 }101 withEnvironment(KotestEngineProperties.filterTests, "g*") {102 SystemPropertyTestFilterEnabledExtension.isEnabled(tc)103 .shouldBe(Enabled.disabled("Excluded by kotest.filter.tests test filter: g.*?"))104 }105 withEnvironment(KotestEngineProperties.filterTests, "*p") {106 SystemPropertyTestFilterEnabledExtension.isEnabled(tc)107 .shouldBe(Enabled.disabled("Excluded by kotest.filter.tests test filter: .*?p"))108 }109 }110 test("should include tests that match pattern in environment variable with underscores") {111 val tc = TestCase(112 SystemPropertyTestFilterEnabledExtensionTest::class.toDescriptor().append("foo"),113 TestName("foo"),114 SystemPropertyTestFilterEnabledExtensionTest(),115 {},116 sourceRef(),117 TestType.Test118 )119 withEnvironment(KotestEngineProperties.filterTests.replace('.', '_'), "foo") {120 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)121 }122 withEnvironment(KotestEngineProperties.filterTests.replace('.', '_'), "f*") {123 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)124 }125 withEnvironment(KotestEngineProperties.filterTests.replace('.', '_'), "*o") {126 SystemPropertyTestFilterEnabledExtension.isEnabled(tc).shouldBe(Enabled.enabled)127 }128 }129 test("should exclude tests that do not match pattern in environment variable with underscores") {130 val tc = TestCase(131 SystemPropertyTestFilterEnabledExtensionTest::class.toDescriptor().append("foo"),132 TestName("foo"),133 SystemPropertyTestFilterEnabledExtensionTest(),134 {},135 sourceRef(),136 TestType.Test137 )138 withEnvironment(KotestEngineProperties.filterTests.replace('.', '_'), "goo") {139 SystemPropertyTestFilterEnabledExtension.isEnabled(tc)140 .shouldBe(Enabled.disabled("Excluded by kotest.filter.tests test filter: goo"))141 }142 withEnvironment(KotestEngineProperties.filterTests.replace('.', '_'), "g*") {143 SystemPropertyTestFilterEnabledExtension.isEnabled(tc)144 .shouldBe(Enabled.disabled("Excluded by kotest.filter.tests test filter: g.*?"))145 }146 withEnvironment(KotestEngineProperties.filterTests.replace('.', '_'), "*p") {147 SystemPropertyTestFilterEnabledExtension.isEnabled(tc)148 .shouldBe(Enabled.disabled("Excluded by kotest.filter.tests test filter: .*?p"))149 }150 }151 }152}...
SystemPropertyTestFilterEnabledExtension.kt
Source:SystemPropertyTestFilterEnabledExtension.kt
1package io.kotest.engine.test.status2import io.kotest.core.descriptors.Descriptor3import io.kotest.core.filter.TestFilter4import io.kotest.core.filter.TestFilterResult5import io.kotest.core.internal.KotestEngineProperties6import io.kotest.core.test.Enabled7import io.kotest.core.test.TestCase8import io.kotest.mpp.Logger9import io.kotest.mpp.syspropOrEnv10/**11 * Applies test and spec filters using sysprop or env vars from [KotestEngineProperties.filterTests]12 * and [KotestEngineProperties.filterSpecs].13 *14 * These work similarly to gradle filters in --tests described at:15 * https://docs.gradle.org/current/userguide/java_testing.html#full_qualified_name_pattern16 */17internal object SystemPropertyTestFilterEnabledExtension : TestEnabledExtension {18 private val logger = Logger(SystemPropertyTestFilterEnabledExtension::class)19 override fun isEnabled(testCase: TestCase): Enabled {20 val filter = syspropOrEnv(KotestEngineProperties.filterTests) ?: ""21 logger.log { Pair(testCase.name.testName, "Filter tests syspropOrEnv=$filter") }22 val excluded = filter23 .propertyToRegexes()24 .map { it.toTestFilter().filter(testCase.descriptor) }25 .filterIsInstance<TestFilterResult.Exclude>()26 .firstOrNull()27 logger.log { Pair(testCase.name.testName, "excluded = $excluded") }28 return if (excluded == null) Enabled.enabled else Enabled.disabled(excluded.reason)29 }30}31private fun Regex.toTestFilter(): TestFilter = object : TestFilter {32 override fun filter(descriptor: Descriptor): TestFilterResult {33 val name = descriptor.path(false).value34 return if (this@toTestFilter.matches(name))35 TestFilterResult.Include36 else37 TestFilterResult.Exclude("Excluded by kotest.filter.tests test filter: ${this@toTestFilter}")38 }39}40private fun String.propertyToRegexes(): List<Regex> =41 this.split(",")42 .filter { it.isNotBlank() }43 .map { it.replace("*", ".*?").toRegex() }...
enabled.kt
Source:enabled.kt
...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,...
filter
Using AI Code Generation
1val result = filter.filter(listOf(testCase))2val result = filter.filter(listOf(testCase))3val result = filter.filter(listOf(testCase))4val result = filter.filter(listOf(testCase))5val result = filter.filter(listOf(testCase))6val result = filter.filter(listOf(testCase))7val result = filter.filter(listOf(testCase))8val result = filter.filter(listOf(testCase))9val result = filter.filter(listOf(testCase))10val result = filter.filter(listOf(testCase))
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!!