How to use JUnitEngineExecutionListenerAdapter class of org.spekframework.spek2.junit package

Best Spek code snippet using org.spekframework.spek2.junit.JUnitEngineExecutionListenerAdapter

ReadmeTestEngine.kt

Source:ReadmeTestEngine.kt Github

copy

Full Screen

...3import ch.tutteli.niok.exists4import ch.tutteli.niok.readText5import ch.tutteli.niok.writeText6import org.junit.platform.engine.*7import org.spekframework.spek2.junit.JUnitEngineExecutionListenerAdapter8import org.spekframework.spek2.junit.SpekTestDescriptor9import org.spekframework.spek2.junit.SpekTestDescriptorFactory10import org.spekframework.spek2.junit.SpekTestEngine11import org.spekframework.spek2.runtime.SpekRuntime12import org.spekframework.spek2.runtime.execution.ExecutionRequest13import java.nio.file.Paths14import java.util.*15import kotlin.collections.HashSet16import kotlin.collections.component117import kotlin.collections.component218import kotlin.collections.filterIsInstance19import kotlin.collections.forEach20import kotlin.collections.isNotEmpty21import kotlin.collections.map22import kotlin.collections.mutableMapOf23import kotlin.collections.set24import org.junit.platform.engine.ExecutionRequest as JUnitExecutionRequest25class ReadmeTestEngine : TestEngine {26 private val spek = SpekTestEngine()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)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())...

Full Screen

Full Screen

SpekTestEngine.kt

Source:SpekTestEngine.kt Github

copy

Full Screen

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

Spek2ForgivingTestEngine.kt

Source:Spek2ForgivingTestEngine.kt Github

copy

Full Screen

1package ch.tutteli.atrium.bctest.spek2import org.junit.platform.engine.*3import org.spekframework.spek2.junit.JUnitEngineExecutionListenerAdapter4import org.spekframework.spek2.junit.SpekTestDescriptor5import org.spekframework.spek2.junit.SpekTestDescriptorFactory6import org.spekframework.spek2.junit.SpekTestEngine7import org.spekframework.spek2.runtime.SpekRuntime8import org.spekframework.spek2.runtime.execution.ExecutionRequest9import java.util.*10import org.junit.platform.engine.ExecutionRequest as JUnitExecutionRequest11class 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)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}...

Full Screen

Full Screen

ReadmeExecutionListener.kt

Source:ReadmeExecutionListener.kt Github

copy

Full Screen

1package readme.examples2import org.spekframework.spek2.junit.JUnitEngineExecutionListenerAdapter3import org.spekframework.spek2.runtime.execution.ExecutionListener4import org.spekframework.spek2.runtime.execution.ExecutionResult5import org.spekframework.spek2.runtime.scope.TestScopeImpl6class ReadmeExecutionListener(7 private val listener: JUnitEngineExecutionListenerAdapter,8 private val examples: MutableMap<String, String>,9 private val code: MutableSet<String>10) : ExecutionListener by listener {11 override fun testExecutionFinish(test: TestScopeImpl, result: ExecutionResult) {12 when (result) {13 ExecutionResult.Success -> handleSuccess(test)14 is ExecutionResult.Failure -> handleFailure(result, test)15 }16 }17 private fun handleSuccess(test: TestScopeImpl) {18 if (!test.id.name.startsWith("code")) {19 listener.testExecutionFinish(20 test,21 ExecutionResult.Failure(IllegalStateException("example tests are supposed to fail"))...

Full Screen

Full Screen

JUnitEngineExecutionListenerAdapter.kt

Source:JUnitEngineExecutionListenerAdapter.kt Github

copy

Full Screen

...5import org.spekframework.spek2.runtime.execution.ExecutionResult6import org.spekframework.spek2.runtime.scope.GroupScopeImpl7import org.spekframework.spek2.runtime.scope.ScopeImpl8import org.spekframework.spek2.runtime.scope.TestScopeImpl9class JUnitEngineExecutionListenerAdapter(10 private val listener: EngineExecutionListener,11 private val factory: SpekTestDescriptorFactory12) : ExecutionListener {13 companion object {14 private const val DEFAULT_IGNORE_REASON = "<no reason provided>"15 }16 override fun executionStart() = Unit17 override fun executionFinish() = Unit18 override fun testExecutionStart(test: TestScopeImpl) {19 listener.executionStarted(test.asJUnitDescriptor())20 }21 override fun testExecutionFinish(test: TestScopeImpl, result: ExecutionResult) {22 listener.executionFinished(test.asJUnitDescriptor(), result.asJUnitResult())23 }...

Full Screen

Full Screen

Spek2ForgivingExecutionListener.kt

Source:Spek2ForgivingExecutionListener.kt Github

copy

Full Screen

1package ch.tutteli.atrium.bctest.spek2import org.spekframework.spek2.junit.JUnitEngineExecutionListenerAdapter3import org.spekframework.spek2.runtime.execution.ExecutionListener4import org.spekframework.spek2.runtime.execution.ExecutionResult5import org.spekframework.spek2.runtime.scope.GroupScopeImpl6import org.spekframework.spek2.runtime.scope.TestScopeImpl7class Spek2ForgivingExecutionListener(8 private val listener: JUnitEngineExecutionListenerAdapter,9 private val forgiveRegex: Regex10) : ExecutionListener by listener {11 override fun groupExecutionFinish(group: GroupScopeImpl, result: ExecutionResult) {12 when (result) {13 ExecutionResult.Success -> listener.groupExecutionFinish(group, result)14 is ExecutionResult.Failure -> handleGroupFailure(result, group)15 }16 }17 private fun handleGroupFailure(result: ExecutionResult.Failure, group: GroupScopeImpl) {18 if (forgiveRegex.matches(group.path.toString())) {19 println("forgiving ${group.path}")20 listener.groupExecutionFinish(group, ExecutionResult.Success)21 } else {22 println("!!!!! path of group in case you want to forgive it failing:\n${group.path}")...

Full Screen

Full Screen

JUnitEngineExecutionListenerAdapter

Using AI Code Generation

copy

Full Screen

1import org.junit.platform.engine.TestExecutionResult2import org.junit.platform.engine.support.descriptor.MethodSource3import org.junit.platform.launcher.TestExecutionListener4import org.junit.platform.launcher.TestIdentifier5import org.spekframework.spek2.junit.JUnitEngineExecutionListenerAdapter6import org.spekframework.spek2.runtime.execution.ExecutionListener7import org.spekframework.spek2.runtime.execution.ExecutionResult8import org.spekframework.spek2.runtime.scope.TestScopeImpl9import org.spekframework.spek2.runtime.scope.TestStatus10class MyListener : ExecutionListener, TestExecutionListener {11 private val adapter = JUnitEngineExecutionListenerAdapter(this)12 override fun testFinished(test: TestScopeImpl) {13 val result = when (test.status) {14 TestStatus.Success -> TestExecutionResult.successful()15 TestStatus.Failure -> TestExecutionResult.failed(test.error)16 TestStatus.Ignored -> TestExecutionResult.aborted(test.error)17 }18 adapter.executionFinished(test.id, result)19 }20 override fun testStarted(test: TestScopeImpl) {21 adapter.executionStarted(test.id, MethodSource.from(test.id))22 }23 override fun executionFinished(identifier: TestIdentifier, result: TestExecutionResult) {24 }25 override fun executionStarted(identifier: TestIdentifier) {26 }27 override fun executionSkipped(identifier: TestIdentifier, reason: String?) {28 }29}30test {31 useJUnitPlatform {32 }33 testLogging {34 }35}

Full Screen

Full Screen

JUnitEngineExecutionListenerAdapter

Using AI Code Generation

copy

Full Screen

1import org.junit.platform.engine.discovery.DiscoverySelectors2import org.junit.platform.launcher.Launcher3import org.junit.platform.launcher.LauncherDiscoveryRequest4import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder5import org.junit.platform.launcher.core.LauncherFactory6import org.spekframework.spek2.junit.JUnitEngineExecutionListenerAdapter7fun main(args: Array<String>) {8 val launcher: Launcher = LauncherFactory.create()9 val request: LauncherDiscoveryRequest = LauncherDiscoveryRequestBuilder.request()10 .selectors(DiscoverySelectors.selectClass(MyTest::class.java))11 .build()12 launcher.execute(request, JUnitEngineExecutionListenerAdapter())13}14import org.junit.platform.engine.discovery.DiscoverySelectors15import org.junit.platform.launcher.Launcher16import org.junit.platform.launcher.LauncherDiscoveryRequest17import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder18import org.junit.platform.launcher.core.LauncherFactory19import org.spekframework.spek2.junit.JUnitPlatform20fun main(args: Array<String>) {21 val launcher: Launcher = LauncherFactory.create()22 val request: LauncherDiscoveryRequest = LauncherDiscoveryRequestBuilder.request()23 .selectors(DiscoverySelectors.selectClass(MyTest::class.java))24 .build()25 launcher.execute(request, JUnitPlatform())26}27import org.junit.platform.engine.discovery.DiscoverySelectors28import org.junit.platform.launcher.Launcher29import org.junit.platform.launcher.LauncherDiscoveryRequest30import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder31import org.junit.platform.launcher.core.LauncherFactory32import org.spekframework.spek2.junit.JUnitPlatformProvider33fun main(args: Array<String>) {34 val launcher: Launcher = LauncherFactory.create()35 val request: LauncherDiscoveryRequest = LauncherDiscoveryRequestBuilder.request()

Full Screen

Full Screen

JUnitEngineExecutionListenerAdapter

Using AI Code Generation

copy

Full Screen

1import org.spekframework.spek2.runtime.execution.ExecutionListener2import org.spekframework.spek2.runtime.execution.ExecutionResult3import org.spekframework.spek2.runtime.scope.TestScope4class JUnitEngineExecutionListenerAdapter(val listener: ExecutionListener) : ExecutionListener {5 override fun executionStart() {6 listener.executionStart()7 }8 override fun executionFinish() {9 listener.executionFinish()10 }11 override fun testExecutionStart(test: TestScope) {12 listener.testExecutionStart(test)13 }14 override fun testExecutionFinish(test: TestScope, result: ExecutionResult) {15 listener.testExecutionFinish(test, result)16 }17 override fun testIgnored(test: TestScope, reason: String?) {18 listener.testIgnored(test, reason)19 }20 override fun testAssumptionFailure(test: TestScope, reason: String?) {21 listener.testAssumptionFailure(test, reason)22 }23 override fun executionSkipped(test: TestScope, reason: String?) {24 listener.executionSkipped(test, reason)25 }26 override fun executionFailure(test: TestScope, throwable: Throwable) {27 listener.executionFailure(test, throwable)28 }29}30import org.junit.platform.engine.TestExecutionResult31import org.junit.platform.engine.reporting.ReportEntry32import org.junit.platform.launcher.TestExecutionListener33import org.junit.platform.launcher.TestIdentifier34import org.junit.platform.launcher.TestPlan35import org.spekframework.spek2.runtime.execution.ExecutionResult36import org.spekframework.spek2.runtime.scope.TestScope37class JUnitEngineExecutionListener(val listener: TestExecutionListener) : ExecutionListener {38 override fun executionStart() {39 listener.testPlanExecutionStarted(TestPlan.from(emptyList()))40 }41 override fun executionFinish() {42 listener.testPlanExecutionFinished(TestPlan.from(emptyList()))43 }44 override fun testExecutionStart(test: TestScope) {45 listener.executionStarted(TestIdentifier.from(test))46 }47 override fun testExecutionFinish(test: TestScope, result: ExecutionResult) {48 listener.executionFinished(TestIdentifier.from(test), createTestExecutionResult(result))49 }50 override fun testIgnored(test: TestScope

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful