Best Spek code snippet using org.spekframework.ide.ServiceMessageAdapter.String.toServiceMessageSafeString
ServiceMessageAdapter.kt
Source:ServiceMessageAdapter.kt
1package org.spekframework.ide2import org.spekframework.spek2.runtime.execution.ExecutionListener3import org.spekframework.spek2.runtime.execution.ExecutionResult4import org.spekframework.spek2.runtime.scope.GroupScopeImpl5import org.spekframework.spek2.runtime.scope.Path6import org.spekframework.spek2.runtime.scope.TestScopeImpl7import java.io.CharArrayWriter8import java.io.PrintWriter9class ServiceMessageAdapter: ExecutionListener {10 private val durations = mutableMapOf<Path, Long>()11 override fun executionStart() { }12 override fun executionFinish() {13 durations.clear()14 }15 override fun testExecutionStart(test: TestScopeImpl) {16 durations[test.path] = System.currentTimeMillis()17 out("testStarted name='${test.path.name.toServiceMessageSafeString()}' locationHint='spek://${test.path.serialize()}'")18 }19 override fun testExecutionFinish(test: TestScopeImpl, result: ExecutionResult) {20 val name = test.path.name.toServiceMessageSafeString()21 val duration = System.currentTimeMillis() - durations[test.path]!!22 if (result is ExecutionResult.Failure) {23 val exceptionDetails = getExceptionDetails(result)24 out("testFailed name='$name' duration='$duration' message='${exceptionDetails.first}' details='${exceptionDetails.second}'")25 }26 out("testFinished name='$name' duration='$duration'")27 }28 override fun testIgnored(test: TestScopeImpl, reason: String?) {29 val name = test.path.name.toServiceMessageSafeString()30 out("testIgnored name='$name' ignoreComment='${reason ?: "no reason provided"}'")31 out("testFinished name='$name'")32 }33 override fun groupExecutionStart(group: GroupScopeImpl) {34 out("testSuiteStarted name='${group.path.name.toServiceMessageSafeString()}' locationHint='spek://${group.path.serialize()}'")35 }36 override fun groupExecutionFinish(group: GroupScopeImpl, result: ExecutionResult) {37 val name = group.path.name.toServiceMessageSafeString()38 if (result is ExecutionResult.Failure) {39 val exceptionDetails = getExceptionDetails(result)40 // fake a child test41 out("testStarted name='$name'")42 out("testFailed name='$name' message='${exceptionDetails.first}' details='${exceptionDetails.second}'")43 out("testFinished name='$name'")44 }45 out("testSuiteFinished name='$name'")46 }47 override fun groupIgnored(group: GroupScopeImpl, reason: String?) {48 val name = group.path.name.toServiceMessageSafeString()49 out("testIgnored name='$name' ignoreComment='${reason ?: "no reason provided"}'")50 out("testFinished name='$name'")51 }52 private fun getExceptionDetails(result: ExecutionResult.Failure): Pair<String?, String> {53 val throwable = result.cause54 val writer = CharArrayWriter()55 throwable.printStackTrace(PrintWriter(writer))56 val details = CharArrayWriter().let {57 throwable.printStackTrace(PrintWriter(it))58 it.toString().toServiceMessageSafeString()59 }60 val message = throwable.message?.toServiceMessageSafeString()61 return message to details62 }63 private fun String.toServiceMessageSafeString(): String {64 return this.replace("|", "||")65 .replace("\n", "|n")66 .replace("\r", "|r")67 .replace("'", "|'")68 .replace("[", "|[")69 .replace("]", "|]")70 .replace(Regex("""\\u(\d\d\d\d)""")) {71 "|0x${it.groupValues[1]}"72 }73 }74 private fun out(event: String) {75 /* ensure service message has it's own line*/76 println()77 println("##teamcity[$event]")78 }79}...
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!!