Best Kotest code snippet using io.kotest.runner.junit.platform.KotestJunitPlatformTestEngine.EngineDiscoveryRequest.postFilters
KotestJunitPlatformTestEngine.kt
Source:KotestJunitPlatformTestEngine.kt
1package io.kotest.runner.junit.platform2import io.kotest.core.config.ProjectConfiguration3import io.kotest.core.extensions.Extension4import io.kotest.core.filter.TestFilter5import io.kotest.core.spec.Spec6import io.kotest.engine.TestEngineLauncher7import io.kotest.engine.listener.PinnedSpecTestEngineListener8import 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 else106 it is MethodSelector107 }108 val descriptor = if (!containsUnsupported) {109 val discovery = Discovery(emptyList())110 val result = discovery.discover(request.toKotestDiscoveryRequest())111 KotestEngineDescriptor(112 uniqueId,113 result.specs,114 result.scripts,115 listOf(gradleClassMethodTestFilter),116 result.error117 )118 } else {119 KotestEngineDescriptor(uniqueId, emptyList(), emptyList(), emptyList(), null)120 }121 logger.log { Pair(null, "JUnit discovery completed [descriptor=$descriptor]") }122 return descriptor123 }124}125class KotestEngineDescriptor(126 id: UniqueId,127 val classes: List<KClass<out Spec>>,128 val scripts: List<KClass<*>>,129 val testFilters: List<TestFilter>,130 val error: Throwable?, // an error during discovery131) : EngineDescriptor(id, "Kotest") {132 override fun mayRegisterTests(): Boolean = true133}134fun EngineDiscoveryRequest.engineFilters() = when (this) {135 is LauncherDiscoveryRequest -> engineFilters.toList()136 else -> emptyList()137}138fun EngineDiscoveryRequest.postFilters() = when (this) {139 is LauncherDiscoveryRequest -> postDiscoveryFilters.toList()140 else -> emptyList()141}...
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!!