How to use filter method of io.kotest.engine.launcher.TestPathTestCaseFilter class

Best Kotest code snippet using io.kotest.engine.launcher.TestPathTestCaseFilter.filter

TestPathTestCaseFilterTest.kt

Source:TestPathTestCaseFilterTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.launcher2import io.kotest.core.descriptors.append3import io.kotest.core.descriptors.toDescriptor4import io.kotest.core.filter.TestFilterResult5import io.kotest.core.spec.style.FunSpec6import io.kotest.core.spec.style.StringSpec7import io.kotest.core.spec.style.WordSpec8import io.kotest.engine.launcher.TestPathTestCaseFilter9import io.kotest.matchers.shouldBe10class TestPathTestCaseFilterTest : FunSpec() {11 init {12 test("filter should exclude tests in a different spec") {13 TestPathTestCaseFilter("foo", Spec1::class).filter(14 Spec2::class.toDescriptor().append("foo")15 ) shouldBe TestFilterResult.Exclude("Excluded by test path filter: 'foo'")16 }17 test("filter should exclude tests in the same spec with a different name") {18 TestPathTestCaseFilter("foo", Spec1::class).filter(19 Spec1::class.toDescriptor().append("boo")20 ) shouldBe TestFilterResult.Exclude("Excluded by test path filter: 'foo'")21 }22 test("filter should include tests matching name and spec") {23 TestPathTestCaseFilter("foo", Spec1::class).filter(Spec1::class.toDescriptor().append("foo")) shouldBe TestFilterResult.Include24 }25 test("filter should include child tests of the target") {26 TestPathTestCaseFilter("foo", Spec1::class).filter(Spec1::class.toDescriptor().append("foo").append("bar")) shouldBe TestFilterResult.Include27 }28 test("filter should include parent tests of the target") {29 TestPathTestCaseFilter("foo -- bar", Spec1::class).filter(30 Spec1::class.toDescriptor().append("foo")31 ) shouldBe TestFilterResult.Include32 }33 test("filter should include the target spec") {34 TestPathTestCaseFilter(35 "foo -- bar",36 Spec1::class37 ).filter(Spec1::class.toDescriptor()) shouldBe TestFilterResult.Include38 }39 test("filter should exclude another spec with same test name") {40 TestPathTestCaseFilter(41 "foo -- bar",42 Spec1::class43 ).filter(Spec2::class.toDescriptor()) shouldBe TestFilterResult.Exclude("Excluded by test path filter: 'foo -- bar'")44 }45 test("filter should work for word spec") {46 TestPathTestCaseFilter("a container -- pass a test", WordSpec1::class).filter(47 WordSpec1::class.toDescriptor().append("a container should").append("pass a test")48 ) shouldBe TestFilterResult.Include49 TestPathTestCaseFilter("a container -- pass a test", WordSpec1::class).filter(50 WordSpec1::class.toDescriptor().append("a container should").append("skip a test")51 ) shouldBe TestFilterResult.Exclude("Excluded by test path filter: 'a container -- pass a test'")52 }53 test("filter should work for word spec with when") {54 TestPathTestCaseFilter("a when", WordSpec2::class).filter(55 WordSpec2::class.toDescriptor().append("a when")56 ) shouldBe TestFilterResult.Include57 TestPathTestCaseFilter("a when -- a should", WordSpec2::class).filter(58 WordSpec2::class.toDescriptor().append("a when").append("a should")59 ) shouldBe TestFilterResult.Include60 TestPathTestCaseFilter("a when -- a should", WordSpec2::class).filter(61 WordSpec2::class.toDescriptor().append("a when").append("a shouldnt")62 ) shouldBe TestFilterResult.Exclude("Excluded by test path filter: 'a when -- a should'")63 TestPathTestCaseFilter("a when -- a should -- a test", WordSpec2::class).filter(64 WordSpec2::class.toDescriptor().append("a when").append("a should").append("a test")65 ) shouldBe TestFilterResult.Include66 TestPathTestCaseFilter("a when -- a should -- a test", WordSpec2::class).filter(67 WordSpec2::class.toDescriptor().append("a when").append("a should").append("boo")68 ) shouldBe TestFilterResult.Exclude("Excluded by test path filter: 'a when -- a should -- a test'")69 }70 test("filter should trim whitespace from names") {71 TestPathTestCaseFilter(" a container ", WordSpec1::class).filter(72 WordSpec1::class.toDescriptor().append("a container should").append("pass a test")73 ) shouldBe TestFilterResult.Include74 TestPathTestCaseFilter(" a container -- pass a test", WordSpec1::class).filter(75 WordSpec1::class.toDescriptor().append("a container should").append("pass a test")76 ) shouldBe TestFilterResult.Include77 }78 }79}80private class Spec1 : StringSpec() {81 init {82 "foo"{}83 "boo"{}84 }85}86private class Spec2 : StringSpec() {87 init {88 "foo"{}...

Full Screen

Full Screen

launcher.kt

Source:launcher.kt Github

copy

Full Screen

2import io.kotest.core.TagExpression3import io.kotest.core.descriptors.Descriptor4import io.kotest.core.descriptors.append5import io.kotest.core.descriptors.toDescriptor6import io.kotest.core.filter.TestFilter7import io.kotest.core.filter.TestFilterResult8import io.kotest.core.spec.Spec9import io.kotest.engine.TestEngineLauncher10import io.kotest.engine.listener.TestEngineListener11import io.kotest.framework.discovery.Discovery12import io.kotest.framework.discovery.DiscoveryRequest13import io.kotest.framework.discovery.DiscoveryResult14import io.kotest.framework.discovery.DiscoverySelector15import kotlin.reflect.KClass16/**17 * Creates a [TestEngineLauncher] to be used to launch the test engine.18 */19internal fun setupLauncher(20 args: LauncherArgs,21 listener: TestEngineListener,22): Result<TestEngineLauncher> = runCatching {23 val specClass = args.spec?.let { (Class.forName(it) as Class<Spec>).kotlin }24 val (specs, _, error) = specs(specClass, args.packageName)25 val filter = if (args.testpath == null || specClass == null) null else {26 TestPathTestCaseFilter(args.testpath, specClass)27 }28 if (error != null) throw error29 TestEngineLauncher(listener)30 .withExtensions(listOfNotNull(filter))31 .withTagExpression(args.tagExpression?.let { TagExpression(it) })32 .withClasses(specs)33}34/**35 * Returns the spec classes to execute by using an FQN class name, a package scan,36 * or a full scan.37 */38private fun specs(specClass: KClass<out Spec>?, packageName: String?): DiscoveryResult {39 // if the spec class was null, then we perform discovery to locate all the classes40 // otherwise that specific spec class is used41 return when (specClass) {42 null -> scan(packageName)43 else -> DiscoveryResult(listOf(specClass), emptyList(), null)44 }45}46private fun scan(packageName: String?): DiscoveryResult {47 val packageSelector = packageName?.let { DiscoverySelector.PackageDiscoverySelector(it) }48 val req = DiscoveryRequest(selectors = listOfNotNull(packageSelector))49 val discovery = Discovery(emptyList())50 return discovery.discover(req)51}52/**53 * Compares test descriptions to a given test path (delimited with ' -- ').54 * The comparison ignores test prefixes, so an application using the launcher should not55 * include test name prefixes in the test path.56 */57private class TestPathTestCaseFilter(58 private val testPath: String,59 spec: KClass<out Spec>,60) : TestFilter {61 private val target1 = testPath.trim().split(Descriptor.TestDelimiter)62 .fold(spec.toDescriptor() as Descriptor) { desc, name ->63 desc.append(name.trim())64 }65 // this is a hack where we append "should" to the first name, until 5.0 where we will66 // store names with affixes separately (right now word spec is adding them to the names at source)67 var should = true68 private val target2 = testPath.trim().split(Descriptor.TestDelimiter)69 .fold(spec.toDescriptor() as Descriptor) { desc, name ->70 if (should) {71 should = false72 desc.append("$name should")73 } else desc.append(name.trim())74 }75 override fun filter(descriptor: Descriptor): TestFilterResult {76 return when {77 target1.isOnPath(descriptor) ||78 target2.isOnPath(descriptor) ||79 descriptor.isOnPath(target1) ||80 descriptor.isOnPath(target2) -> TestFilterResult.Include81 else -> TestFilterResult.Exclude("Excluded by test path filter: '$testPath'")82 }83 }84}

Full Screen

Full Screen

TestPathTestCaseFilter.kt

Source:TestPathTestCaseFilter.kt Github

copy

Full Screen

2import io.kotest.core.descriptors.Descriptor3import io.kotest.core.descriptors.Descriptor.Companion.TestDelimiter4import io.kotest.core.descriptors.append5import io.kotest.core.descriptors.toDescriptor6import io.kotest.core.filter.TestFilter7import io.kotest.core.filter.TestFilterResult8import io.kotest.core.spec.Spec9import kotlin.reflect.KClass10/**11 * Compares test descriptions to a given test path (delimited with ' -- ').12 * The comparison ignores test prefixes, so an application using the launcher should not13 * include test name prefixes in the test path.14 */15class TestPathTestCaseFilter(16 private val testPath: String,17 spec: KClass<out Spec>,18) : TestFilter {19 private val target1 = testPath.trim().split(TestDelimiter)20 .fold(spec.toDescriptor() as Descriptor) { desc, name ->21 desc.append(name.trim())22 }23 // this is a hack where we append "should" to the first name, until 5.0 where we will24 // store names with affixes separately (right now word spec is adding them to the names at source)25 var should = true26 private val target2 = testPath.trim().split(TestDelimiter)27 .fold(spec.toDescriptor() as Descriptor) { desc, name ->28 if (should) {29 should = false30 desc.append("$name should")31 } else desc.append(name.trim())32 }33 override fun filter(descriptor: Descriptor): TestFilterResult {34 return when {35 target1.isOnPath(descriptor) ||36 target2.isOnPath(descriptor) ||37 descriptor.isOnPath(target1) ||38 descriptor.isOnPath(target2) -> TestFilterResult.Include39 else -> TestFilterResult.Exclude("Excluded by test path filter: '$testPath'")40 }41 }42}...

Full Screen

Full Screen

filter

Using AI Code Generation

copy

Full Screen

1val filter = TestPathTestCaseFilter(listOf("io.kotest.engine.launcher.TestPathTestCaseFilterTest"))2val engine = TestEngineLauncher()3engine.withTestFilter(filter)4engine.withTestPath("io.kotest.engine.launcher.TestPathTestCaseFilterTest")5engine.withClasses(MyTestClass::class)6engine.execute()7fun “My test case” () {8}9val filter = TestPathTestCaseFilter("io.kotest.engine.launcher.TestPathTestCaseFilterTest.My test case")10val engine = TestEngineLauncher()11engine.withTestFilter(filter)12engine.withTestPath("io.kotest.engine.launcher.TestPathTestCaseFilterTest")13engine.withClasses(MyTestClass::class)14engine.execute()

Full Screen

Full Screen

filter

Using AI Code Generation

copy

Full Screen

1val testPathTestCaseFilter = TestPathTestCaseFilter(testPath)2val engineLauncher = EngineLauncher()3val engineDiscoveryRequest = EngineDiscoveryRequest()4engineDiscoveryRequest.filters.add(testPathTestCaseFilter)5engineLauncher.execute(engineDiscoveryRequest)6val testPathTestCaseFilter = TestPathTestCaseFilter(testPath)7val engineLauncher = EngineLauncher()8val engineDiscoveryRequest = EngineDiscoveryRequest()9engineDiscoveryRequest.filters.add(testPathTestCaseFilter)10engineLauncher.execute(engineDiscoveryRequest)11val engineDiscoveryRequest = EngineDiscoveryRequest()12val engineLauncher = EngineLauncher()13val testPathTestCaseFilter = TestPathTestCaseFilter(testPath)14engineDiscoveryRequest.filters.add(testPathTestCaseFilter)15engineLauncher.execute(engineDiscoveryRequest)16val engineDiscoveryRequest = EngineDiscoveryRequest()17val engineLauncher = EngineLauncher()18val testPathTestCaseFilter = TestPathTestCaseFilter(testPath)19engineDiscoveryRequest.filters.add(testPathTestCaseFilter)20engineLauncher.execute(engineDiscoveryRequest)21val engineDiscoveryRequest = EngineDiscoveryRequest()22val engineLauncher = EngineLauncher()23val testPathTestCaseFilter = TestPathTestCaseFilter(testPath)24engineDiscoveryRequest.filters.add(testPathTestCaseFilter)25engineLauncher.execute(engineDiscoveryRequest)26val engineDiscoveryRequest = EngineDiscoveryRequest()27val engineLauncher = EngineLauncher()28val testPathTestCaseFilter = TestPathTestCaseFilter(testPath)29engineDiscoveryRequest.filters.add(testPathTestCaseFilter)30engineLauncher.execute(engineDiscoveryRequest)31val engineDiscoveryRequest = EngineDiscoveryRequest()32val engineLauncher = EngineLauncher()33val testPathTestCaseFilter = TestPathTestCaseFilter(testPath)34engineDiscoveryRequest.filters.add(testPathTestCaseFilter)35engineLauncher.execute(engineDiscoveryRequest)36val engineDiscoveryRequest = EngineDiscoveryRequest()37val engineLauncher = EngineLauncher()38val testPathTestCaseFilter = TestPathTestCaseFilter(testPath)39engineDiscoveryRequest.filters.add(testPathTestCaseFilter)40engineLauncher.execute(engineDiscovery

Full Screen

Full Screen

filter

Using AI Code Generation

copy

Full Screen

1val filter = TestPathTestCaseFilter ( setOf ( "com.example.MyTest" , "com.example.MyTest2" ))2val testEngine = TestEngineLauncher ( filter )3val result = testEngine . execute ( testSuite )4val filter = TagTestCaseFilter ( setOf ( "slow" , "fast" ))5val testEngine = TestEngineLauncher ( filter )6val result = testEngine . execute ( testSuite )7val filter = TestCaseFilter { testCase ->8testCase . description . name . contains ( "test" )9}10val testEngine = TestEngineLauncher ( filter )11val result = testEngine . execute ( testSuite )12val filter = TestPathTestCaseFilter ( setOf ( "com.example.MyTest" , "com.example.MyTest2" ))13val testEngine = TestEngineLauncher ( filter )14val result = testEngine . execute ( testSuite )15val filter = TagTestCaseFilter ( setOf ( "slow" , "fast" ))16val testEngine = TestEngineLauncher ( filter )17val result = testEngine . execute ( testSuite )18val filter = TestCaseFilter { testCase ->19testCase . description . name . contains ( "test" )20}21val testEngine = TestEngineLauncher ( filter )22val result = testEngine . execute ( testSuite )23val filter = TestPathTestCaseFilter ( setOf ( "com.example.MyTest" , "com.example.MyTest2" ))24val testEngine = TestEngineLauncher ( filter )25val result = testEngine . execute ( testSuite )26val filter = TagTestCaseFilter ( setOf ( "slow" , "fast" ))27val testEngine = TestEngineLauncher ( filter )

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 TestPathTestCaseFilter

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful