Best Spek code snippet using org.spekframework.spek2.junit.SpekTestEngine.execute
ReadmeTestEngine.kt
Source:ReadmeTestEngine.kt
...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)47 request.fail(t)48 }49 }50 private fun processExamples(classes: List<String>, request: JUnitExecutionRequest) {51 val specContents = classes.map { qualifiedClass ->52 qualifiedClass to fileContent("src/main/kotlin/$qualifiedClass.kt", request)53 }54 specContents.forEach { (_, specContent) ->55 extractSnippets(specContent, request)56 }57 var readmeContent = fileContent(readmeStringPath, request)58 if (examples.isEmpty()) {59 request.fail("no examples found")60 return61 }62 if (code.isEmpty()) {63 request.fail("no code found")64 return65 }66 if (snippets.isEmpty()) {67 request.fail("no snippets found")68 return69 }70 examples.forEach { (exampleId, output) ->71 readmeContent = updateExampleLikeInReadme(readmeContent, specContents, exampleId, output, request)72 }73 code.forEach { codeId ->74 readmeContent = updateCodeInReadme(readmeContent, specContents, codeId, request)75 }76 snippets.forEach { (snippetId, snippetContent) ->77 readmeContent = updateSnippetInReadme(readmeContent, snippetId, snippetContent, request)78 }79 Paths.get(readmeStringPath).writeText(readmeContent)80 }81 private fun extractSnippets(specContent: String, request: JUnitExecutionRequest) {82 Regex("//(snippet-.+)-start([\\S\\s]*?)//(snippet-.+)-end").findAll(specContent).forEach {83 val (tag, content, endTag) = it.destructured84 request.failIf(tag != endTag) { "tag $tag-start did not end with $tag-end but with $endTag" }85 snippets[tag] = content.trimIndent()86 }87 }88 private fun runSpekWithCustomListener(request: JUnitExecutionRequest) : List<String> {89 val roots = request.rootTestDescriptor.children90 .filterIsInstance<SpekTestDescriptor>()91 .map(SpekTestDescriptor::scope)92 val executionListener = ReadmeExecutionListener(93 JUnitEngineExecutionListenerAdapter(request.engineExecutionListener, SpekTestDescriptorFactory()),94 examples,95 code96 )97 val executionRequest = ExecutionRequest(roots, executionListener)98 SpekRuntime().execute(executionRequest)99 return roots.map { it.path.toString() }100 }101 private fun fileContent(path: String, request: JUnitExecutionRequest): String {102 val file = Paths.get(path)103 request.failIf(!file.exists) { "could not find ${file.absolutePathAsString}" }104 return file.readText()105 }106 private inline fun JUnitExecutionRequest.failIf(predicate: Boolean, errorMessage: () -> String) {107 if (predicate) fail(errorMessage())108 }109 private fun JUnitExecutionRequest.fail(errorMessage: String) = fail(IllegalStateException(errorMessage))110 private fun JUnitExecutionRequest.fail(throwable: Throwable) {111 engineExecutionListener.executionFinished(112 rootTestDescriptor,...
SpekTestEngine.kt
Source:SpekTestEngine.kt
...79 .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
...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)32 request.fail(t)33 }34 }35 private fun runSpekWithCustomListener(request: JUnitExecutionRequest) {36 val roots = request.rootTestDescriptor.children37 .filterIsInstance<SpekTestDescriptor>()38 .map(SpekTestDescriptor::scope)39 val executionListener = Spek2ForgivingExecutionListener(40 JUnitEngineExecutionListenerAdapter(request.engineExecutionListener, SpekTestDescriptorFactory()),41 forgiveRegex42 )43 val executionRequest = ExecutionRequest(roots, executionListener)44 SpekRuntime().execute(executionRequest)45 }46 private fun JUnitExecutionRequest.fail(throwable: Throwable) {47 engineExecutionListener.executionFinished(48 rootTestDescriptor,49 TestExecutionResult.failed(throwable)50 )51 }52}...
execute
Using AI Code Generation
1val engine = SpekTestEngine()2val launcher = LauncherFactory.create()3launcher.registerTestEngine(engine)4val request = LauncherDiscoveryRequestBuilder.request().selectors(DiscoverySelectors.selectClass(MySpec.class)).build()5launcher.execute(request)6}7}8import org.junit.platform.engine.discovery.DiscoverySelectors9import org.junit.platform.launcher.LauncherDiscoveryRequest10import org.junit.platform.launcher.LauncherDiscoveryRequestBuilder11import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder12import org.junit.platform.launcher.core.LauncherFactory13import org.spekframework.spek2.junit.SpekTestEngine14import org.spekframework.spek2.runtime.SpekRuntime15import org.spekframework.spek2.runtime.execution.ExecutionListener16import org.spekframework.spek2.runtime.execution.ExecutionResult17import org.spekframework.spek2.runtime.scope.Path18import org.spekframework.spek2.runtime.scope.TestScope19import org.spekframework.spek2.runtime.scope.TestScopeImpl20import org.spekframework.spek2.runtime.scope.TestStatus21import org.spekframework.spek2.runtime.scope.toPath22import org.spekframework.spek2.runtime.scope.toTestScope23import kotlin.test.assertEquals24import kotlin.test.assertTrue25import kotlin.test.fail26object MySpec : Spek({27test("test1") {28}29test("test2") {30}31}) class TestKitExample {32fun test1() {
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!!