Best Kotest code snippet using io.kotest.core.extensions.DiscoveryExtension
Discovery.kt
Source:Discovery.kt
1@file:Suppress("unused")2package io.kotest.framework.discovery3import io.github.classgraph.ClassGraph4import io.github.classgraph.ClassInfo5import io.kotest.core.extensions.DiscoveryExtension6import io.kotest.core.internal.KotestEngineProperties7import io.kotest.core.spec.Spec8import io.kotest.mpp.log9import io.kotest.mpp.syspropOrEnv10import java.lang.management.ManagementFactory11import java.util.concurrent.ConcurrentHashMap12import kotlin.reflect.KClass13//import kotlin.script.templates.standard.ScriptTemplateWithArgs14/**15 * Contains the results of a discovery request scan.16 *17 * @specs these are classes which extend one of the spec types18 * @scripts these are kotlin scripts which may or may not contain tests19 */20data class DiscoveryResult(21 val specs: List<KClass<out Spec>>,22 val scripts: List<KClass<*>>,23 val error: Throwable?, // this error is set if there was an exception during discovery24) {25 companion object {26 fun error(t: Throwable): DiscoveryResult = DiscoveryResult(emptyList(), emptyList(), t)27 }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 ->...
DiscoveryExtension.kt
Source:DiscoveryExtension.kt
...9 *10 * This extension is only usable on the JVM target and will have no effect11 * if gradle or another build tool performs the discovery action.12 *13 * Note: If multiple [DiscoveryExtension]s are registered, the order14 * in which they execute is not specified.15 */16@Deprecated("Discovery extensions cannot be injected into some platforms. Use TestFilter or SpecFilter extensions and integrate via project config. Deprecated since 5.0")17interface DiscoveryExtension : Extension {18 /**19 * Invoked as soon as the scan phase has completed. At this point,20 * the [Spec] classes have been detected, but not yet21 * instantiated or executed.22 *23 * Overriding this function gives implementations the possibility24 * of filtering the specs seen by the test engine.25 *26 * For instance, a possible extension may filter tests by package27 * name, class name, classes that only implement a certain28 * interface, etc.29 *30 * @param classes the [KClass] for each discovered [Spec]31 *...
DiscoveryExtensionExceptionTest.kt
Source:DiscoveryExtensionExceptionTest.kt
1package com.sksamuel.kotest.engine.exts2import io.kotest.core.extensions.DiscoveryExtension3import io.kotest.core.spec.Spec4import io.kotest.core.spec.style.FunSpec5import io.kotest.engine.TestEngineLauncher6import io.kotest.engine.listener.CollectingTestEngineListener7import io.kotest.matchers.booleans.shouldBeTrue8import kotlin.reflect.KClass9/**10 * Asserts that an exception through in a [DiscoveryExtension] is correctly handled.11 */12class DiscoveryExtensionExceptionTest : FunSpec() {13 init {14 test("an exception in a discovery extension should be handled") {15 val collector = CollectingTestEngineListener()16 TestEngineLauncher(collector).withClasses(Dummy::class).launch()17 collector.errors.shouldBeTrue()18 }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}...
DiscoveryExtension
Using AI Code Generation
1class MyDiscoveryExtension : DiscoveryExtension {2override fun afterScan(classes: List<KClass<*>>): List<KClass<*>> {3return classes.filterNot { it.simpleName?.endsWith("Test") ?: false }4}5}6registerExtension(MyDiscoveryExtension())7runBlocking {8val result = EngineLauncher()9.withSpec(MySpec::class)10.run()11println(result)12}13TestSuiteResult(14TestCaseResult(15TestSuiteResult(16TestCaseResult(17TestSuiteResult(18TestCaseResult(19TestSuiteResult(20TestCaseResult(21TestSuiteResult(22TestCaseResult(
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!!