Best Spek code snippet using org.spekframework.spek2.runtime.execution.DiscoveryRequest
TestSupport.kt
Source:TestSupport.kt
2import org.spekframework.spek2.dsl.Root3import org.spekframework.spek2.runtime.SpekRuntime4import org.spekframework.spek2.runtime.discovery.DiscoveryContext5import org.spekframework.spek2.runtime.discovery.DiscoveryContextBuilder6import org.spekframework.spek2.runtime.execution.DiscoveryRequest7import org.spekframework.spek2.runtime.execution.ExecutionListener8import org.spekframework.spek2.runtime.execution.ExecutionRequest9import org.spekframework.spek2.runtime.execution.ExecutionResult10import org.spekframework.spek2.runtime.scope.GroupScopeImpl11import org.spekframework.spek2.runtime.scope.Path12import org.spekframework.spek2.runtime.scope.PathBuilder13import org.spekframework.spek2.runtime.scope.TestScopeImpl14import kotlin.test.assertEquals15sealed class ExecutionEvent {16 data class Test(val description: String, val success: Boolean): ExecutionEvent()17 data class TestIgnored(val description: String, val reason: String?): ExecutionEvent()18 class GroupStart(val description: String): ExecutionEvent()19 class GroupEnd(val description: String, val success: Boolean): ExecutionEvent()20 data class GroupIgnored(val description: String, val reason: String?): ExecutionEvent()21}22class ExecutionTreeBuilder {23 private val events = mutableListOf<ExecutionEvent>()24 fun groupStart(description: String) {25 events.add(ExecutionEvent.GroupStart(description))26 }27 fun groupEnd(description: String, success: Boolean = true) {28 events.add(ExecutionEvent.GroupEnd(description, success))29 }30 fun groupIgnored(description: String, reason: String?) {31 events.add(ExecutionEvent.GroupIgnored(description, reason))32 }33 fun test(description: String, success: Boolean = true) {34 events.add(ExecutionEvent.Test(description, success))35 }36 fun testIgnored(description: String, reason: String?) {37 events.add(ExecutionEvent.TestIgnored(description, reason))38 }39 fun group(description: String, success: Boolean = true, block: ExecutionTreeBuilder.() -> Unit) {40 groupStart(description)41 this.block()42 groupEnd(description, success)43 }44 fun build() = events.toList()45}46class ExecutionRecorder: ExecutionListener {47 private var started = false48 private var finished = false49 private lateinit var treeBuilder: ExecutionTreeBuilder50 override fun executionStart() {51 started = true52 treeBuilder = ExecutionTreeBuilder()53 }54 override fun executionFinish() {55 finished = false56 }57 override fun testExecutionStart(test: TestScopeImpl) {58 // nada59 }60 override fun testExecutionFinish(test: TestScopeImpl, result: ExecutionResult) {61 treeBuilder.test(test.path.name, result is ExecutionResult.Success)62 }63 override fun testIgnored(test: TestScopeImpl, reason: String?) {64 treeBuilder.testIgnored(test.path.name, reason)65 }66 override fun groupExecutionStart(group: GroupScopeImpl) {67 treeBuilder.groupStart(group.path.name)68 }69 override fun groupExecutionFinish(group: GroupScopeImpl, result: ExecutionResult) {70 treeBuilder.groupEnd(group.path.name, result is ExecutionResult.Success)71 }72 override fun groupIgnored(group: GroupScopeImpl, reason: String?) {73 treeBuilder.groupIgnored(group.path.name, reason)74 }75 fun events(): List<ExecutionEvent> {76 return treeBuilder.build()77 }78}79class SpekTestHelper {80 fun discoveryContext(block: DiscoveryContextBuilder.() -> Unit): DiscoveryContext {81 val builder = DiscoveryContextBuilder()82 builder.block()83 return builder.build()84 }85 fun executeTests(context: DiscoveryContext, vararg paths: Path): ExecutionRecorder {86 val runtime = SpekRuntime()87 val recorder = ExecutionRecorder()88 val discoveryResult = runtime.discover(DiscoveryRequest(context, listOf(*paths)))89 runtime.execute(ExecutionRequest(discoveryResult.roots, recorder))90 return recorder91 }92 fun<T: Spek> executeTest(test: T): ExecutionRecorder {93 val path = PathBuilder.from(test::class)94 .build()95 return executeTests(discoveryContext {96 addTest(test::class) { test }97 }, path)98 }99 fun assertExecutionEquals(actual: List<ExecutionEvent>, block: ExecutionTreeBuilder.() -> Unit) {100 val builder = ExecutionTreeBuilder()101 builder.block()102 val expected = builder.build()...
ConsoleLauncher.kt
Source:ConsoleLauncher.kt
1package org.spekframework.spek2.launcher2import org.spekframework.spek2.launcher.reporter.BasicConsoleReporter3import org.spekframework.spek2.runtime.SpekRuntime4import org.spekframework.spek2.runtime.discovery.DiscoveryContext5import org.spekframework.spek2.runtime.execution.DiscoveryRequest6import org.spekframework.spek2.runtime.execution.ExecutionListener7import org.spekframework.spek2.runtime.execution.ExecutionRequest8import org.spekframework.spek2.runtime.execution.ExecutionResult9import org.spekframework.spek2.runtime.scope.GroupScopeImpl10import org.spekframework.spek2.runtime.scope.Path11import org.spekframework.spek2.runtime.scope.PathBuilder12import org.spekframework.spek2.runtime.scope.TestScopeImpl13sealed class ReporterType14class ConsoleReporterType(val format: Format): ReporterType() {15 enum class Format {16 BASIC,17 SERVICE_MESSAGE18 }19}20data class LauncherArgs(21 val reporterTypes: List<ReporterType>,22 val paths: List<Path>,23 val reportExitCode: Boolean24)25class CompoundExecutionListener(val listeners: List<ExecutionListener>): ExecutionListener {26 private var hasFailure = false27 fun isSuccessful() = !hasFailure28 override fun executionStart() {29 listeners.forEach { it.executionStart() }30 }31 override fun executionFinish() {32 listeners.forEach { it.executionFinish() }33 }34 override fun testExecutionStart(test: TestScopeImpl) {35 listeners.forEach { it.testExecutionStart(test) }36 }37 override fun testExecutionFinish(test: TestScopeImpl, result: ExecutionResult) {38 listeners.forEach { it.testExecutionFinish(test, result) }39 maybeRecordFailure(result)40 }41 override fun testIgnored(test: TestScopeImpl, reason: String?) {42 listeners.forEach { it.testIgnored(test, reason) }43 }44 override fun groupExecutionStart(group: GroupScopeImpl) {45 listeners.forEach { it.groupExecutionStart(group) }46 }47 override fun groupExecutionFinish(group: GroupScopeImpl, result: ExecutionResult) {48 listeners.forEach { it.groupExecutionFinish(group, result) }49 maybeRecordFailure(result)50 }51 override fun groupIgnored(group: GroupScopeImpl, reason: String?) {52 listeners.forEach { it.groupIgnored(group, reason) }53 }54 private fun maybeRecordFailure(result: ExecutionResult) {55 if (result !is ExecutionResult.Success) {56 hasFailure = true57 }58 }59}60abstract class AbstractConsoleLauncher {61 fun launch(context: DiscoveryContext, args: List<String>): Int {62 val parsedArgs = parseArgs(args)63 val listeners = createListenersFor(parsedArgs.reporterTypes)64 val runtime = SpekRuntime()65 val paths = mutableListOf<Path>()66 paths.addAll(parsedArgs.paths)67 // todo: empty paths implies run everything68 if (paths.isEmpty()) {69 paths.add(PathBuilder.ROOT)70 }71 val discoveryRequest = DiscoveryRequest(context, paths)72 val discoveryResult = runtime.discover(discoveryRequest)73 val listener = CompoundExecutionListener(listeners)74 val executionRequest = ExecutionRequest(discoveryResult.roots, listener)75 runtime.execute(executionRequest)76 return when {77 parsedArgs.reportExitCode && !listener.isSuccessful() -> -178 parsedArgs.reportExitCode && listener.isSuccessful() -> 079 else -> 080 }81 }82 private fun createListenersFor(reporterTypes: List<ReporterType>): List<ExecutionListener> {83 return reporterTypes.map { reporter ->84 when (reporter) {85 is ConsoleReporterType -> when (reporter.format) {...
SpekTestEngine.kt
Source:SpekTestEngine.kt
1package org.spekframework.spek2.junit2import org.junit.platform.engine.EngineDiscoveryRequest3import org.junit.platform.engine.TestDescriptor4import org.junit.platform.engine.TestEngine5import org.junit.platform.engine.UniqueId6import org.junit.platform.engine.discovery.*7import org.spekframework.spek2.runtime.JvmDiscoveryContextFactory8import org.spekframework.spek2.runtime.SpekRuntime9import org.spekframework.spek2.runtime.execution.DiscoveryRequest10import org.spekframework.spek2.runtime.execution.ExecutionRequest11import org.spekframework.spek2.runtime.scope.Path12import org.spekframework.spek2.runtime.scope.PathBuilder13import java.lang.reflect.Modifier14import java.nio.file.Paths15import org.junit.platform.engine.ExecutionRequest as JUnitExecutionRequest16class SpekTestEngine : TestEngine {17 companion object {18 const val ID = "spek2"19 // Spek does not know how to handle these selectors, fallback to no matching tests.20 private val UNSUPPORTED_SELECTORS = listOf(21 MethodSelector::class.java,22 FileSelector::class.java,23 ModuleSelector::class.java,24 ClasspathResourceSelector::class.java,25 UniqueIdSelector::class.java,26 UriSelector::class.java,27 DirectorySelector::class.java28 )29 }30 private val descriptorFactory = SpekTestDescriptorFactory()31 private val runtime by lazy { SpekRuntime() }32 override fun getId() = ID33 override fun discover(discoveryRequest: EngineDiscoveryRequest, uniqueId: UniqueId): TestDescriptor {34 val engineDescriptor = SpekEngineDescriptor(uniqueId, id)35 if (containsUnsupportedSelector(discoveryRequest)) {36 return engineDescriptor37 }38 val sourceDirs = discoveryRequest.getSelectorsByType(ClasspathRootSelector::class.java)39 .map { it.classpathRoot }40 .map { Paths.get(it) }41 .map { it.toString() }42 val classSelectors = discoveryRequest.getSelectorsByType(ClassSelector::class.java)43 .filter {44 // get all super classes45 val superClasses = mutableListOf<String>()46 var current = it.javaClass.superclass47 while (current != null) {48 superClasses.add(current.name)49 current = current.superclass50 }51 superClasses.contains("org.spekframework.spek2.Spek")52 }53 .filter {54 !(it.javaClass.isAnonymousClass55 || it.javaClass.isLocalClass56 || it.javaClass.isSynthetic57 || Modifier.isAbstract(it.javaClass.modifiers))58 }.map {59 PathBuilder60 .from(it.javaClass.kotlin)61 .build()62 }63 val packageSelectors = discoveryRequest.getSelectorsByType(PackageSelector::class.java)64 .map {65 PathBuilder()66 .appendPackage(it.packageName)67 .build()68 }69 val filters = linkedSetOf<Path>()70 filters.addAll(classSelectors)71 filters.addAll(packageSelectors)72 // todo: empty filter should imply root73 if (filters.isEmpty()) {74 filters.add(PathBuilder.ROOT)75 }76 val context = JvmDiscoveryContextFactory.create(sourceDirs)77 val discoveryResult = runtime.discover(DiscoveryRequest(context, filters.toList()))78 discoveryResult.roots79 .map { descriptorFactory.create(it) }80 .forEach(engineDescriptor::addChild)81 return engineDescriptor82 }83 override fun execute(request: JUnitExecutionRequest) {84 val roots = request.rootTestDescriptor.children85 .filterIsInstance<SpekTestDescriptor>()86 .map(SpekTestDescriptor::scope)87 val executionRequest = ExecutionRequest(88 roots, JUnitEngineExecutionListenerAdapter(request.engineExecutionListener, descriptorFactory)89 )90 runtime.execute(executionRequest)91 }92 private fun containsUnsupportedSelector(discoveryRequest: EngineDiscoveryRequest): Boolean {93 for (selector in UNSUPPORTED_SELECTORS) {94 if (discoveryRequest.getSelectorsByType(selector).isNotEmpty()) {95 return true96 }97 }98 return false99 }100}...
SpekRuntime.kt
Source:SpekRuntime.kt
1package org.spekframework.spek2.runtime2import org.spekframework.spek2.Spek3import org.spekframework.spek2.dsl.Skip4import org.spekframework.spek2.lifecycle.CachingMode5import org.spekframework.spek2.runtime.execution.DiscoveryRequest6import org.spekframework.spek2.runtime.execution.DiscoveryResult7import org.spekframework.spek2.runtime.execution.ExecutionRequest8import org.spekframework.spek2.runtime.lifecycle.LifecycleManager9import org.spekframework.spek2.runtime.scope.*10import org.spekframework.spek2.runtime.util.ClassUtil11private const val DEFAULT_TIMEOUT = 10000L12class SpekRuntime {13 fun discover(discoveryRequest: DiscoveryRequest): DiscoveryResult {14 val scopes = discoveryRequest.context.getTests()15 .map { testInfo ->16 val matchingPath = discoveryRequest.paths.firstOrNull { it.intersects(testInfo.path) }17 testInfo to matchingPath18 }19 .filter { (_, matchingPath) -> matchingPath != null }20 .map { (testInfo, matchingPath) ->21 checkNotNull(matchingPath)22 val spec = resolveSpec(testInfo.createInstance(), testInfo.path)23 spec.filterBy(matchingPath)24 spec25 }26 .filter { spec -> !spec.isEmpty() }27 return DiscoveryResult(scopes)...
Spek2ForgivingTestEngine.kt
Source:Spek2ForgivingTestEngine.kt
...11class Spek2ForgivingTestEngine : TestEngine {12 private val spek = SpekTestEngine()13 private lateinit var forgiveRegex: Regex14 override fun getId(): String = "spek2-forgiving"15 override fun discover(discoveryRequest: EngineDiscoveryRequest, uniqueId: UniqueId): TestDescriptor {16 val descriptor = spek.discover(discoveryRequest, uniqueId)17 val forgive = discoveryRequest.configurationParameters.get("forgive").orElse(null)18 forgiveRegex = Regex(forgive)19 require(descriptor.children.isNotEmpty()) {20 "Could not find any specification, check your runtime classpath"21 }22 return descriptor23 }24 override fun execute(request: JUnitExecutionRequest) {25 val default = Locale.getDefault()26 try {27 Locale.setDefault(Locale.UK)28 runSpekWithCustomListener(request)29 } catch (t: Throwable) {...
console.kt
Source:console.kt
2import com.xenomachina.argparser.ArgParser3import com.xenomachina.argparser.mainBody4import org.spekframework.spek2.runtime.JvmDiscoveryContextFactory5import org.spekframework.spek2.runtime.SpekRuntime6import org.spekframework.spek2.runtime.execution.DiscoveryRequest7import org.spekframework.spek2.runtime.execution.ExecutionRequest8import org.spekframework.spek2.runtime.scope.PathBuilder9class Spek2ConsoleLauncher {10 fun run(args: LauncherArgs) {11 val paths = args.paths.map {12 PathBuilder.parse(it)13 .build()14 }15 val context = JvmDiscoveryContextFactory.create(args.sourceDirs.toList())16 val discoveryRequest = DiscoveryRequest(context, paths)17 val runtime = SpekRuntime()18 val discoveryResult = runtime.discover(discoveryRequest)19 val executionRequest = ExecutionRequest(discoveryResult.roots, ServiceMessageAdapter())20 runtime.execute(executionRequest)21 }22}23class LauncherArgs(parser: ArgParser) {24 val sourceDirs by parser.adding("--sourceDirs", help="Spec source dirs")25 val paths by parser.adding("--paths", help = "Spek paths to execute")26}27fun main(args: Array<String>) = mainBody {28 val launcherArgs = ArgParser(args).parseInto(::LauncherArgs)29 Spek2ConsoleLauncher().run(launcherArgs)30}...
DiscoveryRequest.kt
Source:DiscoveryRequest.kt
1package org.spekframework.spek2.runtime.execution2import org.spekframework.spek2.runtime.JvmDiscoveryContextFactory3import org.spekframework.spek2.runtime.discovery.DiscoveryContext4import org.spekframework.spek2.runtime.scope.Path5actual class DiscoveryRequest actual constructor(actual val context: DiscoveryContext,6 actual val paths: List<Path>) {7 // TODO: remove on the next release after 2.0.1, it's only here for compatibility8 constructor(testDirs: List<String>, paths: List<Path>): this(JvmDiscoveryContextFactory.create(testDirs), paths)9}...
Discovery.kt
Source:Discovery.kt
1package org.spekframework.spek2.runtime.execution2import org.spekframework.spek2.runtime.discovery.DiscoveryContext3import org.spekframework.spek2.runtime.scope.Path4import org.spekframework.spek2.runtime.scope.ScopeImpl5expect class DiscoveryRequest(context: DiscoveryContext, paths: List<Path>) {6 val context: DiscoveryContext7 val paths: List<Path>8}9class DiscoveryResult(val roots: List<ScopeImpl>)...
DiscoveryRequest
Using AI Code Generation
1val discoveryRequest = DiscoveryRequestBuilder().build()2val discoveryResult = discover(discoveryRequest)3val executionRequest = ExecutionRequestBuilder(discoveryResult).build()4val executionResult = execute(executionRequest)5val discoveryRequest = DiscoveryRequestBuilder().build()6val discoveryResult = discover(discoveryRequest)7val executionRequest = ExecutionRequestBuilder(discoveryResult).build()8val executionResult = execute(executionRequest)9val discoveryRequest = DiscoveryRequestBuilder().build()10val discoveryResult = discover(discoveryRequest)11val executionRequest = ExecutionRequestBuilder(discoveryResult).build()12val executionResult = execute(executionRequest)13val discoveryRequest = DiscoveryRequestBuilder().build()14val discoveryResult = discover(discoveryRequest)15val executionRequest = ExecutionRequestBuilder(discoveryResult).build()16val executionResult = execute(executionRequest)17val discoveryRequest = DiscoveryRequestBuilder().build()18val discoveryResult = discover(discoveryRequest)19val executionRequest = ExecutionRequestBuilder(discoveryResult).build()20val executionResult = execute(executionRequest)21val discoveryRequest = DiscoveryRequestBuilder().build()22val discoveryResult = discover(discoveryRequest)23val executionRequest = ExecutionRequestBuilder(discoveryResult).build()24val executionResult = execute(executionRequest)25val discoveryRequest = DiscoveryRequestBuilder().build()26val discoveryResult = discover(discoveryRequest)27val executionRequest = ExecutionRequestBuilder(discoveryResult).build()28val executionResult = execute(executionRequest)29val discoveryRequest = DiscoveryRequestBuilder().build()30val discoveryResult = discover(discoveryRequest)31val executionRequest = ExecutionRequestBuilder(discoveryResult).build()32val executionResult = execute(executionRequest)33val discoveryRequest = DiscoveryRequestBuilder().build()34val discoveryResult = discover(discoveryRequest)35val executionRequest = ExecutionRequestBuilder(discoveryResult).build()36val executionResult = execute(executionRequest)37val discoveryRequest = DiscoveryRequestBuilder().build()38val discoveryResult = discover(discoveryRequest)39val executionRequest = ExecutionRequestBuilder(discoveryResult).build()40val executionResult = execute(executionRequest)41val discoveryRequest = DiscoveryRequestBuilder().build()42val discoveryResult = discover(discoveryRequest)43val executionRequest = ExecutionRequestBuilder(discoveryResult).build()44val executionResult = execute(executionRequest)45val discoveryRequest = DiscoveryRequestBuilder().build
DiscoveryRequest
Using AI Code Generation
1val discoveryRequest = DiscoveryRequestBuilder()2.discoverTestsInClasspath()3.build()4val discoveryResult = discover(discoveryRequest)5val testExecutor = TestExecutor()6val testExecutionResult = testExecutor.execute(discoveryResult)
DiscoveryRequest
Using AI Code Generation
1import org.spekframework.spek2.runtime.execution.DiscoveryRequest2val request = DiscoveryRequest(DiscoveryContext(), TestEngineDiscoveryRequestBuilder.request().selectors(selectClass(MySpec::class.java)).build())3import org.spekframework.spek2.runtime.execution.DiscoverySelectorResolver4val selectors = DiscoverySelectorResolver().resolve(DiscoveryRequest(DiscoveryContext(), TestEngineDiscoveryRequestBuilder.request().selectors(selectClass(MySpec::class.java)).build()))5import org.spekframework.spek2.runtime.execution.Discovery6val request = DiscoveryRequest(DiscoveryContext(), TestEngineDiscoveryRequestBuilder.request().selectors(selectClass(MySpec::class.java)).build())7val selectors = DiscoverySelectorResolver().resolve(request)8val discovery = Discovery(request)9val root = discovery.discover(selectors)10import org.spekframework.spek2.runtime.execution.DiscoveryContext11val context = DiscoveryContext()12import org.spekframework.spek2.runtime.execution.DiscoverySelectorResolver13val selectors = DiscoverySelectorResolver().resolve(DiscoveryRequest(DiscoveryContext(), TestEngineDiscoveryRequestBuilder.request().selectors(selectClass(MySpec::class.java)).build()))14import org.spekframework.spek2.runtime.execution.Discovery15val request = DiscoveryRequest(DiscoveryContext(), TestEngineDiscoveryRequestBuilder.request().selectors(selectClass(MySpec::class.java)).build())16val selectors = DiscoverySelectorResolver().resolve(request)17val discovery = Discovery(request)18val root = discovery.discover(selectors)19import org.spekframework.spek2.runtime.execution.DiscoveryContext20val context = DiscoveryContext()21import org.spekframework.spek2.runtime.execution.DiscoverySelectorResolver22val selectors = DiscoverySelectorResolver().resolve(DiscoveryRequest(DiscoveryContext(), TestEngineDiscoveryRequestBuilder.request().selectors(selectClass(MySpec::class.java)).build()))
DiscoveryRequest
Using AI Code Generation
1val discoveryRequest = DiscoveryRequestBuilder.buildDiscoveryRequest(2 listOf("org.spekframework.spek2.runtime.scope.PathBuilderTest"),3 emptySet(),4 emptySet(),5 emptySet()6val discoverySelectors = DiscoverySelectorResolver().resolve(discoveryRequest)7val testDescriptors = TestDescriptorResolver().resolve(discoverySelectors)8val testCases = TestDescriptorToTestCaseConverter().convert(testDescriptors)9val testExecutions = TestExecutor().execute(testCases)
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!!