Best Spek code snippet using org.spekframework.spek2.junit.SpekTestEngine.discover
ReadmeTestEngine.kt
Source:ReadmeTestEngine.kt
...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)...
SpecificationStyleTest.kt
Source:SpecificationStyleTest.kt
...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)...
SpekTestEngine.kt
Source:SpekTestEngine.kt
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}...
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) {30 t.printStackTrace()31 Locale.setDefault(default)...
discover
Using AI Code Generation
1val testEngine = SpekTestEngine()2val discoveryRequest = TestDiscoveryRequestBuilder.request()3.discoverTests()4.build()5val discoveryResult = testEngine.discover(discoveryRequest, UniqueId.forEngine(testEngine.id))6val testEngine = SpekTestEngine()7val executionRequest = TestExecutionRequestBuilder.request()8.executeTests()9.build()10val executionResult = testEngine.execute(executionRequest, UniqueId.forEngine(testEngine.id))11val testEngine = SpekTestEngine()12val executionRequest = TestExecutionRequestBuilder.request()13.executeTests()14.build()15val executionResult = testEngine.execute(executionRequest, UniqueId.forEngine(testEngine.id))16val testEngine = SpekTestEngine()17val executionRequest = TestExecutionRequestBuilder.request()18.executeTests()19.build()20val executionResult = testEngine.execute(executionRequest, UniqueId.forEngine(testEngine.id))21val testEngine = SpekTestEngine()22val executionRequest = TestExecutionRequestBuilder.request()23.executeTests()24.build()25val executionResult = testEngine.execute(executionRequest, UniqueId.forEngine(testEngine.id))26val testEngine = SpekTestEngine()27val executionRequest = TestExecutionRequestBuilder.request()28.executeTests()29.build()30val executionResult = testEngine.execute(executionRequest, UniqueId.forEngine(testEngine.id))31val testEngine = SpekTestEngine()32val executionRequest = TestExecutionRequestBuilder.request()33.executeTests()34.build()35val executionResult = testEngine.execute(executionRequest, UniqueId.forEngine(testEngine.id))36val testEngine = SpekTestEngine()
discover
Using AI Code Generation
1plugins {2 id("org.spekframework.spek2") version "2.0.15"3}4tasks.withType<Test> {5 useJUnitPlatform()6}7import org.spekframework.spek2.Spek8import org.spekframework.spek2.style.gherkin.Feature9object GreetingSpek : Spek({10 Feature("Greeting") {11 Scenario("say hello") {12 When("I say hello") {
discover
Using AI Code Generation
1val engine = SpekTestEngine()2val request = LauncherDiscoveryRequestBuilder.request()3val launcher = LauncherFactory.create()4val listener = object : TestExecutionListener {5override fun executionFinished(testIdentifier: TestIdentifier, testExecutionResult: TestExecutionResult) {6println("${testIdentifier.displayName} finished with status ${testExecutionResult.status}")7}8}9launcher.registerTestExecutionListeners(listener)10launcher.execute(request)11}12}
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!!