How to use discover method of org.spekframework.spek2.runtime.SpekRuntime class

Best Spek code snippet using org.spekframework.spek2.runtime.SpekRuntime.discover

ReadmeTestEngine.kt

Source:ReadmeTestEngine.kt Github

copy

Full Screen

...27 private val examples = mutableMapOf<String, String>()28 private val code = HashSet<String>()29 private val snippets = mutableMapOf<String, String>()30 override fun getId(): String = "spek2-readme"31 override fun discover(discoveryRequest: EngineDiscoveryRequest, uniqueId: UniqueId): TestDescriptor {32 val descriptor = spek.discover(discoveryRequest, uniqueId)33 require(descriptor.children.isNotEmpty()) {34 "Could not find any specification, check your runtime classpath"35 }36 return descriptor37 }38 override fun execute(request: JUnitExecutionRequest) {39 val default = Locale.getDefault()40 try {41 Locale.setDefault(Locale.UK)42 val classes = runSpekWithCustomListener(request)43 processExamples(classes, request)44 } catch (t: Throwable) {45 t.printStackTrace()46 Locale.setDefault(default)...

Full Screen

Full Screen

TestSupport.kt

Source:TestSupport.kt Github

copy

Full Screen

1package org.spekframework.spek22import 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()103 assertEquals(executionToString(actual), executionToString(expected))104 }105 /**106 * Prints out something like the following107 *108 * foo bar {109 * should do this -> PASSED...

Full Screen

Full Screen

SpecificationStyleTest.kt

Source:SpecificationStyleTest.kt Github

copy

Full Screen

...19 at org.spekframework.spek2.runtime.lifecycle.MemoizedValueAdapter.getValue(MemoizedValueAdapter.kt:22)20 at com.example.spek.SpecificationStyleTest$1.invoke(SpecificationStyleTest.kt:18)21 at com.example.spek.SpecificationStyleTest$1.invoke(SpecificationStyleTest.kt:9)22 at org.spekframework.spek2.runtime.SpekRuntime.resolveSpec(SpekRuntime.kt:49)23 at org.spekframework.spek2.runtime.SpekRuntime.discover(SpekRuntime.kt:23)24 at org.spekframework.spek2.junit.SpekTestEngine.discover(SpekTestEngine.kt:92)25 at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:181)26 ... 31 more27 */28 logger.info("init, list: {}", list)29 describe("desc 1") {30 // cannot access items31// add("desc 1")32// logger.info("desc 1, memoized: {}, list: {}", items, list)33/*34Caused by: java.lang.AssertionError: 'items' can not be accessed in this context.35 at org.spekframework.spek2.runtime.lifecycle.MemoizedValueAdapter.get(MemoizedValueAdapter.kt:33)36 at org.spekframework.spek2.runtime.lifecycle.MemoizedValueAdapter.getValue(MemoizedValueAdapter.kt:22)37 at com.example.spek.SpecificationStyleTest$1$1.invoke(SpecificationStyleTest.kt:16)38 at com.example.spek.SpecificationStyleTest$1$2.invoke(SpecificationStyleTest.kt:34)39 at com.example.spek.SpecificationStyleTest$1$2.invoke(SpecificationStyleTest.kt:9)...

Full Screen

Full Screen

ConsoleLauncher.kt

Source:ConsoleLauncher.kt Github

copy

Full Screen

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) {86 ConsoleReporterType.Format.BASIC -> BasicConsoleReporter()87 else -> throw AssertionError("Unsupported console reporter: ${reporter.format}")88 }...

Full Screen

Full Screen

SpekTestEngine.kt

Source:SpekTestEngine.kt Github

copy

Full Screen

2import 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}...

Full Screen

Full Screen

SpekRuntime.kt

Source:SpekRuntime.kt Github

copy

Full Screen

...9import 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)28 }29 fun execute(request: ExecutionRequest) = Executor().execute(request)30 private fun resolveSpec(instance: Spek, path: Path): GroupScopeImpl {...

Full Screen

Full Screen

Spek2ForgivingTestEngine.kt

Source:Spek2ForgivingTestEngine.kt Github

copy

Full Screen

...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) {30 t.printStackTrace()31 Locale.setDefault(default)...

Full Screen

Full Screen

console.kt

Source:console.kt Github

copy

Full Screen

...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}...

Full Screen

Full Screen

discover

Using AI Code Generation

copy

Full Screen

1val runner = SpekRuntime.discover("com.mypackage")2val runner = SpekRuntime.run("com.mypackage")3val runner = SpekRuntime.run("com.mypackage.MySpec")4val runner = SpekRuntime.run("com.mypackage.MySpec")5val runner = SpekRuntime.run("com.mypackage.MySpec", "My test")6val runner = SpekRuntime.run("com.mypackage.MySpec", "My test")7val runner = SpekRuntime.run("com.mypackage.MySpec", "My test")8val runner = SpekRuntime.run("com.mypackage.MySpec", "My test")9val runner = SpekRuntime.run("com.mypackage.MySpec", "My test")10val runner = SpekRuntime.run("com.mypackage.MySpec", "My test")11val runner = SpekRuntime.run("com.mypackage.MySpec", "My test")

Full Screen

Full Screen

discover

Using AI Code Generation

copy

Full Screen

1val runtime = SpekRuntime()2val engine = JUnitPlatformEngine()3val request = LauncherDiscoveryRequestBuilder.request()4.withDiscoverySelectors(5selectClass(MySpek::class.java),6selectClass(MyOtherSpek::class.java)7.build()8val result = runtime.execute(request, engine)9println("test count: ${result.testCount}")10println("success count: ${result.successCount}")11println("failure count: ${result.failureCount}")12}13}

Full Screen

Full Screen

discover

Using AI Code Generation

copy

Full Screen

1val runtime = SpekRuntime()2val listener = MyRuntimeListener()3val classpath = listOf("/path/to/your/test/")4val classLoader = ClassLoader.getSystemClassLoader()5runtime.discover(classpath, classLoader, listener)6val runtime = SpekRuntime()7val listener = MyRuntimeListener()8val classpath = listOf("/path/to/your/test/")9val classLoader = ClassLoader.getSystemClassLoader()10val filters = emptyList<Filter>()11runtime.execute(classpath, classLoader, listener, filters)12public fun discover(13public fun execute(14public interface RuntimeListener {15fun discover(discoveryContext: DiscoveryContext)16fun execute(executionContext: ExecutionContext)17}18public class DiscoveryContext(19public class ExecutionContext(20public class DiscoveryScope(21public class ExecutionScope(22public class DiscoveryGroup(

Full Screen

Full Screen

discover

Using AI Code Generation

copy

Full Screen

1val discoveryResults = SpekRuntime.discover()2SpekRuntime.run(discoveryResults) { notifier ->3}4SpekRuntime.run(discoveryResults) { notifier ->5}6SpekRuntime.run(discoveryResults) { notifier ->7}8SpekRuntime.run(discoveryResults) { notifier ->9}10SpekRuntime.run(discoveryResults) { notifier ->11}12SpekRuntime.run(discoveryResults) { notifier ->13}14SpekRuntime.run(discoveryResults) { notifier ->15}16SpekRuntime.run(discoveryResults) { notifier ->17}18SpekRuntime.run(discoveryResults) { notifier ->19}20SpekRuntime.run(discoveryResults) { notifier ->21}22SpekRuntime.run(discoveryResults) { notifier ->23}24SpekRuntime.run(discoveryResults) { notifier ->25}26SpekRuntime.run(discoveryResults) { notifier ->27}

Full Screen

Full Screen

discover

Using AI Code Generation

copy

Full Screen

1import org.spekframework.spek2.runtime.SpekRuntime2fun main() {3 val runtime = SpekRuntime()4 val specs = runtime.discover("com.example.spek")5 runtime.run(specs)6}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Spek automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in SpekRuntime

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful