Best Kotest code snippet using io.kotest.engine.launcher.TestPathTestCaseFilter
TestPathTestCaseFilterTest.kt
Source:TestPathTestCaseFilterTest.kt
...4import 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"{}...
launcher.kt
Source:launcher.kt
...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 = false...
TestPathTestCaseFilter.kt
Source:TestPathTestCaseFilter.kt
...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 = false...
TestPathTestCaseFilter
Using AI Code Generation
1val filter = TestPathTestCaseFilter(listOf("MyTest"))2val listener = object : TestEngineListener {3override fun engineStarted(classes: List<KClass<*>>) {4println("Engine started")5}6override fun engineFinished(t: List<Throwable>) {7println("Engine finished")8}9override fun specStarted(kclass: KClass<*>) {10println("Spec started: $kclass")11}12override fun specFinished(kclass: KClass<*>, t: Throwable?) {13println("Spec finished: $kclass")14}15override fun testStarted(testCase: TestCase) {16println("Test started: $testCase")17}18override fun testFinished(testCase: TestCase, result: TestResult) {19println("Test finished: $testCase, result: $result")20}21override fun testIgnored(testCase: TestCase, reason: String?) {22println("Test ignored: $testCase, reason: $reason")23}24}25val engine = TestEngine(listOf(MyTest::class), filter, listener)26engine.execute()27Test finished: MyTest: test1, result: TestResult(status=Success)28Test finished: MyTest: test2, result: TestResult(status=Success)
TestPathTestCaseFilter
Using AI Code Generation
1val testCaseFilter = TestPathTestCaseFilter(listOf("io.kotest.engine.launcher.TestPathTestCaseFilterTest"))2val launcher = KotestEngineLauncher()3val result = launcher.withClasses(TestPathTestCaseFilterTest::class)4.withTestFilters(testCaseFilter)5.launch()6Assertions.assertEquals(1, result.testsStartedCount)7Assertions.assertEquals(1, result.testsPassedCount)8Assertions.assertEquals(0, result.testsFailedCount)9Assertions.assertEquals(0, result.testsIgnoredCount)10Assertions.assertEquals(0, result.testsSkippedCount)11Assertions.assertEquals(0, result.testsAbortedCount)12}13}14}15val testCaseFilter = TestPathTestCaseFilter(listOf("io.kotest.engine.launcher.TestPathTestCaseFilterTest"))16val launcher = KotestEngineLauncher()17val result = launcher.withClasses(TestPathTestCaseFilterTest::class)18.withTestFilters(testCaseFilter)19.launch()20Assertions.assertEquals(1, result.testsStartedCount)21Assertions.assertEquals(1, result.testsPassedCount)22Assertions.assertEquals(0, result.testsFailedCount)23Assertions.assertEquals(0, result.testsIgnoredCount)24Assertions.assertEquals(0, result.testsSkippedCount)25Assertions.assertEquals(0, result.testsAbortedCount)26}27}28}29val testCaseFilter = TestPathTestCaseFilter(listOf("io.kotest.engine.launcher.TestPathTestCaseFilterTest"))30val launcher = KotestEngineLauncher()31val result = launcher.withClasses(TestPathTestCaseFilterTest::class)32.withTestFilters(testCaseFilter)33.launch()34Assertions.assertEquals(1, result.testsStartedCount)35Assertions.assertEquals(1, result.testsPassedCount)36Assertions.assertEquals(0, result.testsFailedCount)37Assertions.assertEquals(0, result.testsIgnoredCount)38Assertions.assertEquals(0, result.testsSkippedCount)39Assertions.assertEquals(0, result.testsAbortedCount)40}41}42}43val testCaseFilter = TestPathTestCaseFilter(listOf("io.kotest.engine.launcher.TestPathTestCaseFilterTest"))44val launcher = KotestEngineLauncher()45val result = launcher.withClasses(TestPathTestCaseFilterTest::class)46.withTestFilters(testCaseFilter)47.launch()48Assertions.assertEquals(1, result.testsStartedCount)49Assertions.assertEquals(1, result.testsPassedCount)50Assertions.assertEquals(
TestPathTestCaseFilter
Using AI Code Generation
1val testPathFilter = TestPathTestCaseFilter(listOf("io.kotest.engine.launcher.TestPathTestCaseFilterTest"))2val engine = TestEngineLauncher()3val listener = MyTestEngineListener()4engine.withTestPathFilter(testPathFilter)5.withTestEngineListener(listener)6.withSpecs(listOf(MySpec::class))7.launch()8val testNameFilter = TestNameTestCaseFilter(listOf("test name"))9val engine = TestEngineLauncher()10val listener = MyTestEngineListener()11engine.withTestNameFilter(testNameFilter)12.withTestEngineListener(listener)13.withSpecs(listOf(MySpec::class))14.launch()15val testTagFilter = TestTagTestCaseFilter(listOf("tag name"))16val engine = TestEngineLauncher()17val listener = MyTestEngineListener()18engine.withTestTagFilter(testTagFilter)19.withTestEngineListener(listener)20.withSpecs(listOf(MySpec::class))21.launch()22val testDescriptionFilter = TestDescriptionTestCaseFilter(listOf("test description"))23val engine = TestEngineLauncher()24val listener = MyTestEngineListener()25engine.withTestDescriptionFilter(testDescriptionFilter)26.withTestEngineListener(listener)27.withSpecs(listOf(MySpec::class))28.launch()29val testNameFilter = TestNameTestCaseFilter(listOf("test name"))30val engine = TestEngineLauncher()31val listener = MyTestEngineListener()32engine.withTestNameFilter(testNameFilter)33.withTestEngineListener(listener)34.withSpecs(listOf(MySpec::class))35.launch()36val testTagFilter = TestTagTestCaseFilter(listOf("tag name"))37val engine = TestEngineLauncher()38val listener = MyTestEngineListener()39engine.withTestTagFilter(testTagFilter)40.withTestEngineListener(listener)41.withSpecs(listOf(MySpec::class))42.launch()43val testDescriptionFilter = TestDescriptionTestCaseFilter(listOf("test description"))44val engine = TestEngineLauncher()45val listener = MyTestEngineListener()
TestPathTestCaseFilter
Using AI Code Generation
1val launcher = TestEngineLauncher()2val filter = TestPathTestCaseFilter(listOf("com.example.MyTest"))3launcher.execute(listOf("com.example"), filter)4val launcher = TestEngineLauncher()5val filter = TestNameTestCaseFilter(listOf("test name"))6launcher.execute(listOf("com.example"), filter)7val launcher = TestEngineLauncher()8val filter = TestTagTestCaseFilter(listOf("tag name"))9launcher.execute(listOf("com.example"), filter)10val launcher = TestEngineLauncher()11val filter = TestTagAndPathTestCaseFilter(listOf("tag name"), listOf("com.example.MyTest"))12launcher.execute(listOf("com.example"), filter)13val launcher = TestEngineLauncher()14val filter = TestTagOrPathTestCaseFilter(listOf("tag name"), listOf("com.example.MyTest"))15launcher.execute(listOf("com.example"), filter)16val launcher = TestEngineLauncher()17val filter = TestTagAndNameTestCaseFilter(listOf("tag name"), listOf("test name"))18launcher.execute(listOf("com.example"), filter)19val launcher = TestEngineLauncher()20val filter = TestTagOrNameTestCaseFilter(listOf("tag name"), listOf("test name"))21launcher.execute(listOf("com.example"), filter)22val launcher = TestEngineLauncher()23val filter = TestTagAndPathAndNameTestCaseFilter(listOf("tag name"), listOf("com.example.MyTest"), listOf("test name"))24launcher.execute(listOf("com.example"), filter)25val launcher = TestEngineLauncher()26val filter = TestTagOrPathOrNameTestCaseFilter(listOf("tag name"), listOf("com.example.MyTest"), listOf("test name"))27launcher.execute(listOf
TestPathTestCaseFilter
Using AI Code Generation
1class TestPathTestCaseFilterTest : FunSpec() {2 init {3 test("should return true for test case with matching path") {4 val filter = TestPathTestCaseFilter(listOf("TestPathTestCaseFilterTest.should return true for test case with matching path"))5 filter.testCase(6 TestCase(7 TestPathTestCaseFilterTest::class.members.first { it.name == "should return true for test case with matching path" },8 TestDescriptor(9 TestPathTestCaseFilterTest::class.members.first { it.name == "should return true for test case with matching path" },10 }11 test("should return false for test case with non matching path") {12 val filter = TestPathTestCaseFilter(listOf("TestPathTestCaseFilterTest.should return false for test case with non matching path"))13 filter.testCase(14 TestCase(15 TestPathTestCaseFilterTest::class.members.first { it.name == "should return true for test case with matching path" },16 TestDescriptor(17 TestPathTestCaseFilterTest::class.members.first { it.name == "should return true for test case with matching path" },18 }19 }20}21class TestPathTestCaseFilterTest : FunSpec() {22 init {23 test("should return true for test case with matching path") {24 val filter = TestPathTestCaseFilter(listOf("TestPathTestCaseFilterTest.should return true for test case with matching path"))25 filter.testCase(26 TestCase(27 TestPathTestCaseFilterTest::class.members.first { it.name == "should return true for test case with matching path" },28 TestDescriptor(29 TestPathTestCaseFilterTest::class.members.first { it.name == "should return true for test case with matching path" },30 }31 test("should return false for test case with non matching path") {32 val filter = TestPathTestCaseFilter(listOf("TestPathTestCaseFilterTest.should return false for test case with
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!!