Best Kotest code snippet using io.kotest.runner.junit.platform.gradle.GradlePostDiscoveryFilterExtractor
KotestJunitPlatformTestEngine.kt
Source:KotestJunitPlatformTestEngine.kt
...8import io.kotest.engine.listener.ThreadSafeTestEngineListener9import io.kotest.framework.discovery.Discovery10import io.kotest.mpp.Logger11import io.kotest.runner.junit.platform.gradle.GradleClassMethodRegexTestFilter12import io.kotest.runner.junit.platform.gradle.GradlePostDiscoveryFilterExtractor13import org.junit.platform.engine.DiscoverySelector14import org.junit.platform.engine.EngineDiscoveryRequest15import org.junit.platform.engine.ExecutionRequest16import org.junit.platform.engine.TestEngine17import org.junit.platform.engine.TestExecutionResult18import org.junit.platform.engine.UniqueId19import org.junit.platform.engine.discovery.MethodSelector20import org.junit.platform.engine.discovery.UniqueIdSelector21import org.junit.platform.engine.support.descriptor.EngineDescriptor22import org.junit.platform.launcher.LauncherDiscoveryRequest23import java.util.*24import kotlin.reflect.KClass25/**26 * A Kotest implementation of a Junit Platform [TestEngine].27 */28class KotestJunitPlatformTestEngine : TestEngine {29 private val logger = Logger(KotestJunitPlatformTestEngine::class)30 companion object {31 const val EngineId = "kotest"32 }33 override fun getId(): String = EngineId34 override fun getGroupId(): Optional<String> = Optional.of("io.kotest")35 override fun execute(request: ExecutionRequest) {36 logger.log { Pair(null, "ExecutionRequest[${request::class.java.name}] [configurationParameters=${request.configurationParameters}; rootTestDescriptor=${request.rootTestDescriptor}]") }37 val root = request.rootTestDescriptor as KotestEngineDescriptor38 when (root.error) {39 null -> execute(request, root)40 else -> abortExecution(request, root.error)41 }42 }43 private fun abortExecution(request: ExecutionRequest, e: Throwable) {44 request.engineExecutionListener.executionStarted(request.rootTestDescriptor)45 request.engineExecutionListener.executionFinished(request.rootTestDescriptor, TestExecutionResult.failed(e))46 }47 private fun execute(request: ExecutionRequest, root: KotestEngineDescriptor) {48 val configuration = ProjectConfiguration()49 val listener = ThreadSafeTestEngineListener(50 PinnedSpecTestEngineListener(51 JUnitTestEngineListener(52 SynchronizedEngineExecutionListener(53 request.engineExecutionListener54 ),55 root,56 )57 )58 )59 request.configurationParameters.get("kotest.extensions").orElseGet { "" }60 .split(',')61 .map { it.trim() }62 .filter { it.isNotBlank() }63 .map { Class.forName(it).newInstance() as Extension }64 .forEach { configuration.registry.add(it) }65 TestEngineLauncher(listener)66 .withConfiguration(configuration)67 .withExtensions(root.testFilters)68 .withClasses(root.classes)69 .launch()70 }71 /**72 * gradlew --tests rules:73 * Classname: adds classname selector and ClassMethodNameFilter post discovery filter74 * Classname.method: adds classname selector and ClassMethodNameFilter post discovery filter75 * org.Classname: doesn't seem to invoke the discover or execute methods.76 *77 * filter in gradle test block:78 * includeTestsMatching("*Test") - class selectors and ClassMethodNameFilter with pattern79 * includeTestsMatching("*Test") AND includeTestsMatching("org.gradle.internal.*") - class selectors and ClassMethodNameFilter with two patterns80 */81 override fun discover(82 request: EngineDiscoveryRequest,83 uniqueId: UniqueId,84 ): KotestEngineDescriptor {85 logger.log { Pair(null, "JUnit discovery request [uniqueId=$uniqueId]") }86 logger.log { Pair(null, request.string()) }87 // if we are excluded from the engines then we say goodnight according to junit rules88 val isKotest = request.engineFilters().all { it.toPredicate().test(this) }89 if (!isKotest)90 return KotestEngineDescriptor(uniqueId, emptyList(), emptyList(), emptyList(), null)91 val classMethodFilterRegexes = GradlePostDiscoveryFilterExtractor.extract(request.postFilters())92 val gradleClassMethodTestFilter = GradleClassMethodRegexTestFilter(classMethodFilterRegexes)93 // a method selector is passed by intellij to run just a single method inside a test file94 // this happens for example, when trying to run a junit test alongside kotest tests,95 // and kotest will then run all other tests.96 // Some other engines run tests via uniqueId selectors97 // therefore, the presence of a MethodSelector or a UniqueIdSelector means we must run no tests in KT.98 // if we get a uniqueid with kotest as engine we throw because that should never happen99 val allSelectors = request.getSelectorsByType(DiscoverySelector::class.java)100 val containsUnsupported = allSelectors.any {101 if (it is UniqueIdSelector)102 if (it.uniqueId.engineId.get() == EngineId)103 throw RuntimeException("Kotest does not allow running tests via uniqueId")104 else true105 else...
GradlePostDiscoveryFilterExtractor.kt
Source:GradlePostDiscoveryFilterExtractor.kt
...15 * to this reflection bullshit to get the raw strings out, so we can parse and apply the patterns ourselves,16 * thus allowing kotest to properly support the --tests options.17 *18 */19object GradlePostDiscoveryFilterExtractor {20 private val logger = Logger(GradlePostDiscoveryFilterExtractor::class)21 fun extract(filters: List<PostDiscoveryFilter>): List<String> {22 val classMethodFilters = filters.filter { it.javaClass.simpleName == "ClassMethodNameFilter" }23 return classMethodFilters.flatMap { extract(it) }24 }25 private fun extract(filter: Any): List<String> = runCatching {26 val matcher = testMatcher(filter)27 logger.log { Pair(null, "TestMatcher [$matcher]") }28 val commandLineIncludePatterns = commandLineIncludePatterns(matcher)29 logger.log { Pair(null, "commandLineIncludePatterns [$commandLineIncludePatterns]") }30 val regexes = commandLineIncludePatterns.map { pattern(it) }31 logger.log { Pair(null, "ClassMethodNameFilter regexes [$regexes]") }32 regexes33 }.getOrElse { emptyList() }34 private fun testMatcher(obj: Any): Any {...
GradlePostDiscoveryFilterExtractor
Using AI Code Generation
1import io.kotest.core.spec.Spec2import io.kotest.runner.junit.platform.gradle.GradlePostDiscoveryFilterExtractor3import org.junit.platform.engine.discovery.DiscoverySelectors4import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder5import org.junit.platform.launcher.core.LauncherFactory6object GradlePostDiscoveryFilterExtractorTest {7 fun main(args: Array<String>) {8 val classes = listOf(9 val launcher = LauncherFactory.create()10 val request = LauncherDiscoveryRequestBuilder.request()11 .selectors(classes.map { DiscoverySelectors.selectClass(it) })12 .build()13 val filter = GradlePostDiscoveryFilterExtractor.extract(request, launcher)14 val filteredSpecs = filter.filter(classes.map { Class.forName(it).kotlin as Class<out Spec> })15 println(filteredSpecs)16 }17}18[kotlin.KotlinReflectionInternalError: Exception while analyzing expression at (1,1) in /home/ranjit/IdeaProjects/kotest/kotest-runner/kotest-runner-junit5/src/test/kotlin/io/kotest/runner/junit5/SpecExecutorTest.kt]
GradlePostDiscoveryFilterExtractor
Using AI Code Generation
1val gradlePostDiscoveryFilterExtractor = GradlePostDiscoveryFilterExtractor(gradleTestFilter)2val kotestTestEngine = KotestTestEngine()3kotestTestEngine.execute(kotestTestFilter)4val gradleTestFilter = GradleTestFilter()5val gradlePostDiscoveryFilterExtractor = GradlePostDiscoveryFilterExtractor(gradleTestFilter)6val kotestTestEngine = KotestTestEngine()7kotestTestEngine.execute(kotestTestFilter)8val gradleTestFilter = GradleTestFilter()9val gradlePostDiscoveryFilterExtractor = GradlePostDiscoveryFilterExtractor(gradleTestFilter)10val kotestTestEngine = KotestTestEngine()11kotestTestEngine.execute(kotestTestFilter)12val gradleTestFilter = GradleTestFilter()13val gradlePostDiscoveryFilterExtractor = GradlePostDiscoveryFilterExtractor(gradleTestFilter)14val kotestTestEngine = KotestTestEngine()15kotestTestEngine.execute(kotestTestFilter)
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!!