Best Kotest code snippet using io.kotest.core.extensions.DiscoveryExtension.afterScan
Discovery.kt
Source:Discovery.kt
...28}29/**30 * Scans for tests as specified by a [DiscoveryRequest].31 *32 * [DiscoveryExtension] `afterScan` functions are applied after the scan is complete to33 * optionally filter the returned classes.34 */35class Discovery(private val discoveryExtensions: List<DiscoveryExtension> = emptyList()) {36 private val requests = ConcurrentHashMap<DiscoveryRequest, DiscoveryResult>()37 // the results of a classpath scan, lazily executed and memoized.38 private val scanResult = lazy { classgraph().scan() }39 // filter functions40 //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 classes...
DiscoveryExtension.kt
Source:DiscoveryExtension.kt
...30 * @param classes the [KClass] for each discovered [Spec]31 *32 * @return the list of filtered classes to use.33 */34 fun afterScan(classes: List<KClass<out Spec>>): List<KClass<out Spec>>35}...
DiscoveryExtensionExceptionTest.kt
Source:DiscoveryExtensionExceptionTest.kt
...19 }20}21private class EmptySpec : FunSpec()22private val ext = object : DiscoveryExtension {23 override fun afterScan(classes: List<KClass<out Spec>>): List<KClass<out Spec>> {24 error("discovery goes boom")25 }26}27private object Dummy : FunSpec() {28 init {29 test("a") {}30 }31}
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!!