Best Kotest code snippet using io.kotest.engine.teamcity.locations.location
TeamCityTestEngineListener.kt
Source:TeamCityTestEngineListener.kt
...72 override suspend fun specStarted(kclass: KClass<*>) {73 val msg = TeamCityMessageBuilder74 .testSuiteStarted(prefix, formatter.format(kclass))75 .id(kclass.toDescriptor().path().value)76 .locationHint(Locations.location(kclass))77 .build()78 println(msg)79 }80 // ignored specs are completely hidden from output in team city81 override suspend fun specIgnored(kclass: KClass<*>, reason: String?) {}82 override suspend fun specFinished(kclass: KClass<*>, result: TestResult) {83 // if the spec itself has an error, we must insert a placeholder test84 when (val t = result.errorOrNull) {85 null -> Unit86 is MultipleExceptions -> t.causes.forEach { insertPlaceholder(it, kclass.toDescriptor()) }87 else -> insertPlaceholder(t, kclass.toDescriptor())88 }89 finishSpec(kclass)90 results.clear()91 children.clear()92 }93 private fun finishSpec(kclass: KClass<*>) {94 val msg = TeamCityMessageBuilder95 .testSuiteFinished(prefix, formatter.format(kclass))96 .id(kclass.toDescriptor().path().value)97 .locationHint(Locations.location(kclass))98 .build()99 println(msg)100 }101 override suspend fun testStarted(testCase: TestCase) {102 if (testCase.parent != null) addChild(testCase)103 when (testCase.type) {104 TestType.Container -> startTestSuite(testCase)105 TestType.Test -> startTest(testCase)106 TestType.Dynamic -> Unit107 }108 }109 override suspend fun testIgnored(testCase: TestCase, reason: String?) {110 ignoreTest(testCase, TestResult.Ignored(reason))111 }112 private fun addChild(testCase: TestCase) {113 children.getOrPut(testCase.descriptor.parent) { mutableListOf() }.add(testCase)114 }115 override suspend fun testFinished(testCase: TestCase, result: TestResult) {116 results[testCase.descriptor] = result117 when (testCase.type) {118 TestType.Container -> {119 failTestSuiteIfError(testCase, result)120 finishTestSuite(testCase, result)121 }122 TestType.Test -> {123 if (!started.contains(testCase.descriptor)) startTest(testCase)124 if (result.isErrorOrFailure) failTest(testCase, result)125 finishTest(testCase, result)126 }127 TestType.Dynamic -> {128 if (isParent(testCase)) {129 startTestSuite(testCase)130 failTestSuiteIfError(testCase, result)131 finishTestSuite(testCase, result)132 } else {133 startTest(testCase)134 if (result.isErrorOrFailure) failTest(testCase, result)135 finishTest(testCase, result)136 }137 }138 }139 }140 private fun failTestSuiteIfError(testCase: TestCase, result: TestResult) {141 // test suites cannot be in a failed state, so we must insert a placeholder to hold any error142 when (val t = result.errorOrNull) {143 null -> Unit144 is MultipleExceptions -> t.causes.forEach { insertPlaceholder(it, testCase.descriptor) }145 else -> insertPlaceholder(t, testCase.descriptor)146 }147 }148 // returns true if this test case is a parent149 private fun isParent(testCase: TestCase) = children.getOrElse(testCase.descriptor) { mutableListOf() }.isNotEmpty()150 /**151 * For a given [TestCase] will output the "test ignored" message.152 */153 private fun ignoreTest(testCase: TestCase, result: TestResult.Ignored) {154 val msg = TeamCityMessageBuilder155 .testIgnored(prefix, formatter.format(testCase))156 .id(testCase.descriptor.path().value)157 .parent(testCase.descriptor.parent.path().value)158 .locationHint(Locations.location(testCase.source))159 .message(result.reason)160 .result(result)161 .build()162 println(msg)163 }164 /**165 * For a [TestCase] will output the "test started" message.166 */167 private fun startTest(testCase: TestCase) {168 val msg = TeamCityMessageBuilder169 .testStarted(prefix, formatter.format(testCase))170 .id(testCase.descriptor.path().value)171 .parent(testCase.descriptor.parent.path().value)172 .locationHint(Locations.location(testCase.source))173 .build()174 println(msg)175 started.add(testCase.descriptor)176 }177 /**178 * For a given [TestCase] will output the "test failed" message.179 */180 private fun failTest(testCase: TestCase, result: TestResult) {181 val msg = TeamCityMessageBuilder182 .testFailed(prefix, formatter.format(testCase))183 .id(testCase.descriptor.path().value)184 .parent(testCase.descriptor.parent.path().value)185 .duration(result.duration)186 .locationHint(Locations.location(testCase.source))187 .withException(result.errorOrNull, details)188 .result(result)189 .build()190 println(msg)191 }192 /**193 * For a given [TestCase] will output the "test finished" message.194 */195 private fun finishTest(testCase: TestCase, result: TestResult) {196 val msg = TeamCityMessageBuilder197 .testFinished(prefix, formatter.format(testCase))198 .id(testCase.descriptor.path().value)199 .parent(testCase.descriptor.parent.path().value)200 .duration(result.duration)201 .locationHint(Locations.location(testCase.source))202 .result(result)203 .build()204 println(msg)205 }206 /**207 * For a given [TestCase] will output the "test suite started" message.208 */209 private fun startTestSuite(testCase: TestCase) {210 val msg = TeamCityMessageBuilder211 .testSuiteStarted(prefix, formatter.format(testCase))212 .id(testCase.descriptor.path().value)213 .parent(testCase.descriptor.parent.path().value)214 .locationHint(Locations.location(testCase.source))215 .build()216 println(msg)217 started.add(testCase.descriptor)218 }219 /**220 * For a given [TestCase] will output the "test suite finished" message.221 */222 private fun finishTestSuite(testCase: TestCase, result: TestResult) {223 val msg = TeamCityMessageBuilder224 .testSuiteFinished(prefix, formatter.format(testCase))225 .id(testCase.descriptor.path().value)226 .parent(testCase.descriptor.parent.path().value)227 .duration(result.duration)228 .locationHint(Locations.location(testCase.source))229 .result(result)230 .build()231 println(msg)232 }233}...
locations.kt
Source:locations.kt
2import io.kotest.core.source.SourceRef3import io.kotest.mpp.bestName4import kotlin.reflect.KClass5object Locations {6 fun location(kclass: KClass<*>): String =7 "kotest:class://" + kclass.bestName() + ":1"8 // note that everything before the :// is considered the "protocol" by the intellij plugin9 private fun fileHint(fileName: String, lineNumber: Int) = "kotest:file://${fileName}:${lineNumber}"10 private fun classHint(fqn: String, lineNumber: Int) = "kotest:class://${fqn}:${lineNumber}"11 fun location(sourceRef: SourceRef): String? = when (sourceRef) {12 is SourceRef.FileSource -> fileHint(sourceRef.fileName, sourceRef.lineNumber ?: 1)13 is SourceRef.ClassSource -> classHint(sourceRef.fqn, sourceRef.lineNumber ?: 1)14 SourceRef.None -> null15 }16}...
LocationsTest.kt
Source:LocationsTest.kt
...4import io.kotest.engine.teamcity.Locations5import io.kotest.matchers.shouldBe6class LocationsTest : FunSpec({7 test("ClassSource hint") {8 Locations.location(SourceRef.ClassSource("foo.bar", null)) shouldBe "kotest:class://foo.bar:1"9 Locations.location(SourceRef.ClassSource("foo.bar", 34)) shouldBe "kotest:class://foo.bar:34"10 }11 test("FileSource hint") {12 Locations.location(SourceRef.FileSource("foo.kt", null)) shouldBe "kotest:file://foo.kt:1"13 Locations.location(SourceRef.FileSource("foo.kt", 34)) shouldBe "kotest:file://foo.kt:34"14 }15})...
location
Using AI Code Generation
1val location = io.kotest.engine.teamcity.locations.location(testCase)2val message = io.kotest.engine.teamcity.messages.message(testCase, result)3val message = io.kotest.engine.teamcity.messages.message(testCase, result)4val message = io.kotest.engine.teamcity.messages.message(testCase, result)5val message = io.kotest.engine.teamcity.messages.message(testCase, result)6val message = io.kotest.engine.teamcity.messages.message(testCase, result)7val message = io.kotest.engine.teamcity.messages.message(testCase, result)8val message = io.kotest.engine.teamcity.messages.message(testCase, result)9val message = io.kotest.engine.teamcity.messages.message(testCase, result)10val message = io.kotest.engine.teamcity.messages.message(testCase, result)11val message = io.kotest.engine.teamcity.messages.message(testCase, result)12val message = io.kotest.engine.teamcity.messages.message(testCase, result)
location
Using AI Code Generation
1val location = locations.find { it.testId == test.id }2if (location != null) {3} else {4print("##teamcity[testStarted name='${test.name}']")5}6}7fun printTeamCityTestFinished(test: TestResult) {8print("##teamcity[testFinished name='${test.name}']")9}10fun printTeamCityTestIgnored(test: TestResult) {11print("##teamcity[testIgnored name='${test.name}']")12}13fun printTeamCityTestFailed(test: TestResult) {14print("##teamcity[testFailed name='${test.name}' message='${test.error?.message?.escape()}' details='${test.error?.stackTraceString()?.escape()}']")15}16fun String.escape() = replace("|", "||")17.replace("'", "|'")18.replace("[", "|[")19.replace("]", "|]")20.replace("\n", "|n")21.replace("\r", "|r")22.replace("\u0085", "|x")23.replace("\u2028", "|l")24.replace("\u2029", "|p")25private fun Throwable.stackTraceString(): String? = StringWriter().use { sw ->26printStackTrace(PrintWriter(sw))27sw.toString()28}29}30fun main(args: Array<String>) {31KotestEngineLauncher()32.engineArgs(args)33.execute()34}35}36class locations {37companion object {38val locations = mutableListOf<Location>()39}40data class Location(val testId: String, val file: String, val line: Int)41}42object Location {43fun of(testId: String, file: String, line: Int) = locations.Location(testId, file, line)44}
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!!