Best Kotest code snippet using io.kotest.framework.discovery.DiscoveryResult.scanUris
Discovery.kt
Source:Discovery.kt
...40 //private val isScript: (KClass<*>) -> Boolean = { ScriptTemplateWithArgs::class.java.isAssignableFrom(it.java) }41 private val isSpecSubclassKt: (KClass<*>) -> Boolean = { Spec::class.java.isAssignableFrom(it.java) }42 private val isSpecSubclass: (Class<*>) -> Boolean = { Spec::class.java.isAssignableFrom(it) }43 private val isAbstract: (KClass<*>) -> Boolean = { it.isAbstract }44 private val fromClassPaths: List<KClass<out Spec>> by lazy { scanUris() }45 /**46 * Returns a function that applies all the [DiscoveryFilter]s to a given class.47 * The class must pass all the filters to be included.48 */49 private fun filterFn(filters: List<DiscoveryFilter>): (KClass<out Spec>) -> Boolean = { kclass ->50 filters.isEmpty() || filters.all { it.test(kclass) }51 }52 /**53 * Returns a function that applies all the [DiscoverySelector]s to a given class.54 * The class must pass any one selector to be included.55 */56 private fun selectorFn(selectors: List<DiscoverySelector>): (KClass<out Spec>) -> Boolean = { kclass ->57 selectors.isEmpty() || selectors.any { it.test(kclass) }58 }59 fun discover(request: DiscoveryRequest): DiscoveryResult =60 requests.getOrPut(request) { doDiscovery(request).getOrElse { DiscoveryResult.error(it) } }61// /**62// * Scans the classpaths for kotlin script files.63// */64// private fun discoverScripts(): List<KClass<out ScriptTemplateWithArgs>> {65// log { "Discovery: Running script scan" }66// return scanResult.value67// .allClasses68// .filter { it.extendsSuperclass(ScriptTemplateWithArgs::class.java.name) }69// .map { it.load(false) }70// .filter(isScript)71// .filterIsInstance<KClass<out ScriptTemplateWithArgs>>()72// }73 /**74 * Loads a class reference from a [ClassInfo].75 *76 * @param init false to avoid initializing the class77 */78 private fun ClassInfo.load(init: Boolean): KClass<out Any> =79 Class.forName(name, init, this::class.java.classLoader).kotlin80 private fun doDiscovery(request: DiscoveryRequest): Result<DiscoveryResult> = runCatching {81 val specClasses =82 if (request.onlySelectsSingleClasses()) loadSelectedSpecs(request) else fromClassPaths83 val filtered = specClasses84 .asSequence()85 .filter(selectorFn(request.selectors))86 .filter(filterFn(request.filters))87 // all classes must subclass one of the spec parents88 .filter(isSpecSubclassKt)89 // we don't want abstract classes90 .filterNot(isAbstract)91 .toList()92 log { "After filters there are ${filtered.size} spec classes" }93 log { "[Discovery] Further filtering classes via discovery extensions [$discoveryExtensions]" }94 val afterExtensions = discoveryExtensions95 .fold(filtered) { cl, ext -> ext.afterScan(cl) }96 .sortedBy { it.simpleName }97 log { "After discovery extensions there are ${afterExtensions.size} spec classes" }98 val scriptsEnabled = System.getProperty(KotestEngineProperties.scriptsEnabled) == "true" ||99 System.getenv(KotestEngineProperties.scriptsEnabled) == "true"100// val scripts = when {101// scriptsEnabled -> discoverScripts()102// else -> emptyList()103// }104 if (scanResult.isInitialized()) runCatching { scanResult.value.close() }105 log { "Discovery result [${afterExtensions.size} specs; scripts]" }106 DiscoveryResult(afterExtensions, emptyList(), null)107 }108 /**109 * Returns whether this is a request that selects single classes110 * only. Used to avoid full classpath scans when not necessary.111 */112 private fun DiscoveryRequest.onlySelectsSingleClasses(): Boolean =113 selectors.isNotEmpty() &&114 selectors.all { it is DiscoverySelector.ClassDiscoverySelector }115 /**116 * Returns a list of [Spec] classes from discovery requests that only have117 * selectors of type [DiscoverySelector.ClassDiscoverySelector].118 */119 private fun loadSelectedSpecs(request: DiscoveryRequest): List<KClass<out Spec>> {120 log { "Discovery: Loading specified classes..." }121 val start = System.currentTimeMillis()122 // first filter down to spec instances only, then load the full class123 val loadedClasses = request124 .selectors125 .asSequence()126 .filterIsInstance<DiscoverySelector.ClassDiscoverySelector>()127 .map { Class.forName(it.className, false, this::class.java.classLoader) }128 .filter(isSpecSubclass)129 .map { Class.forName(it.name).kotlin }130 .filterIsInstance<KClass<out Spec>>()131 .toList()132 val duration = System.currentTimeMillis() - start133 log { "Discovery: Loading of selected classes completed in ${duration}ms" }134 return loadedClasses135 }136 /**137 * Returns a list of [Spec] classes detected using classgraph in the list of138 * locations specified by the uris param.139 */140 private fun scanUris(): List<KClass<out Spec>> {141 log {142 val uptime = ManagementFactory.getRuntimeMXBean().uptime143 "Discovery: Starting test discovery scan... [uptime=$uptime]"144 }145 val result = scanResult.value146 .getSubclasses(Spec::class.java.name)147 .map { Class.forName(it.name).kotlin }148 .filterIsInstance<KClass<out Spec>>()149 log {150 val start = System.currentTimeMillis()151 val duration = System.currentTimeMillis() - start152 "Discovery: Test discovery completed in ${duration}ms"153 }154 return result...
scanUris
Using AI Code Generation
1val discoveryResult = DiscoveryResult.scanUris(listOf("my.package"))2val discoveryResult = DiscoveryResult.scanClasspath()3val discoveryResult = DiscoveryResult.scanClasspath("my.package")4val discoveryResult = DiscoveryResult.scanClasspath(listOf("my.package"))5val discoveryResult = DiscoveryResult.scanClasspath("my.package", "my.package1")6val discoveryResult = DiscoveryResult.scanClasspath(listOf("my.package", "my.package1"))7val discoveryResult = DiscoveryResult.scanClasspath("my.package", listOf("my.package1"))8val discoveryResult = DiscoveryResult.scanClasspath(listOf("my.package"), listOf("my.package1"))9val discoveryResult = DiscoveryResult.scanClasspath(listOf("my.package"), "my.package1")10val discoveryResult = DiscoveryResult.scanClasspath(listOf("my.package", "my.package1"))11val discoveryResult = DiscoveryResult.scanClasspath("my.package", listOf("my.package1"))12val discoveryResult = DiscoveryResult.scanClasspath(listOf("my.package"), listOf("my.package1"))13val discoveryResult = DiscoveryResult.scanClasspath(listOf("my.package"), "my.package1")
scanUris
Using AI Code Generation
1val uris = DiscoveryResult.scanUris()2val uris = DiscoveryResult.scanClasspath()3val uris = DiscoveryResult.scanClasspath()4val uris = DiscoveryResult.scanClasspath()5val uris = DiscoveryResult.scanClasspath()6val uris = DiscoveryResult.scanClasspath()7val uris = DiscoveryResult.scanClasspath()8val uris = DiscoveryResult.scanClasspath()9val uris = DiscoveryResult.scanClasspath()10val uris = DiscoveryResult.scanClasspath()11val uris = DiscoveryResult.scanClasspath()12val uris = DiscoveryResult.scanClasspath()13val uris = DiscoveryResult.scanClasspath()
scanUris
Using AI Code Generation
1val uris = ClasspathDiscovery . findTestUris ( classLoader )2val uris = DirectoryDiscovery . findTestUris ( directory )3val uris = ClassDiscovery . findTestUris ( clazz )4val uris = PackageDiscovery . findTestUris ( packageName )5val uris = ClasspathDiscovery . findTestUris ( classLoader )6val uris = ClasspathDiscovery . findTestUris ( classLoader )7val uris = ClasspathDiscovery . findTestUris ( classLoader )8val uris = ClasspathDiscovery . findTestUris ( classLoader )9val uris = ClasspathDiscovery . findTestUris ( classLoader )
scanUris
Using AI Code Generation
1val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))2val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))3val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))4val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))5val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))6val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))7val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))8val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))9val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))10val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))11val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))12val discoveryResult = DiscoveryResult(ScanResult(listOf(DiscoverySelector("test", "test"))))
scanUris
Using AI Code Generation
1val discoveryResult = DiscoveryResult(scanUris(listOf("classpath:/com/xyz/testcases")))2val config = ProjectConfig()3val testEngine = TestEngine(discoveryResult, config)4val listener = object : TestEngineListener {5override fun testFinished(testCase: TestCase, result: TestResult) {6println("Test finished with result $result")7}8}9testEngine.execute(listener)10}11}
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!!