Best Kotest code snippet using io.kotest.engine.teamcity.locations
TeamCityTestEngineListener.kt
Source:TeamCityTestEngineListener.kt
1package io.kotest.engine.listener2import io.kotest.core.config.ProjectConfiguration3import io.kotest.core.descriptors.Descriptor4import io.kotest.core.descriptors.toDescriptor5import io.kotest.core.names.DisplayNameFormatter6import io.kotest.core.test.TestCase7import io.kotest.core.test.TestResult8import io.kotest.core.test.TestType9import io.kotest.engine.errors.ExtensionExceptionExtractor10import io.kotest.engine.extensions.MultipleExceptions11import io.kotest.engine.interceptors.EngineContext12import io.kotest.engine.teamcity.Locations13import io.kotest.engine.teamcity.TeamCityMessageBuilder14import io.kotest.engine.test.names.DefaultDisplayNameFormatter15import io.kotest.engine.test.names.getDisplayNameFormatter16import io.kotest.mpp.bestName17import kotlin.reflect.KClass18/**19 * A [TestEngineListener] that logs events to the console using a [TeamCityMessageBuilder].20 */21class TeamCityTestEngineListener(22 private val prefix: String = TeamCityMessageBuilder.TeamCityPrefix,23 private val details: Boolean = true,24) : TestEngineListener {25 private var formatter: DisplayNameFormatter = DefaultDisplayNameFormatter(ProjectConfiguration())26 // once a spec has completed, we want to be able to check whether any given test is27 // a container or a leaf test, and so this map contains all test that have children28 private val children = mutableMapOf<Descriptor, MutableList<TestCase>>()29 private val results = mutableMapOf<Descriptor, TestResult>()30 private val started = mutableSetOf<Descriptor.TestDescriptor>()31 // intellij has no method for failed suites, so if a container or spec fails we must insert32 // a dummy "test" in order to tag the error against that33 private fun insertPlaceholder(t: Throwable, parent: Descriptor) {34 val (name, cause) = ExtensionExceptionExtractor.resolve(t)35 val msg1 = TeamCityMessageBuilder36 .testStarted(prefix, name)37 .id(name)38 .parent(parent.path().value)39 .build()40 println(msg1)41 // we must print out the stack trace in between the dummy, so it appears when you click on the test name42 //t?.printStackTrace()43 val msg2 = TeamCityMessageBuilder44 .testFailed(prefix, name)45 .id(name)46 .parent(parent.path().value)47 .withException(cause, details)48 .build()49 println(msg2)50 val msg3 = TeamCityMessageBuilder51 .testFinished(prefix, name)52 .id(name)53 .parent(parent.path().value)54 .build()55 println(msg3)56 }57 override suspend fun engineStarted() {}58 override suspend fun engineInitialized(context: EngineContext) {59 formatter = getDisplayNameFormatter(context.configuration.registry, context.configuration)60 }61 override suspend fun engineFinished(t: List<Throwable>) {62 if (t.isNotEmpty()) {63 t.withIndex().forEach { (index, error) ->64 val testName = if (t.size == 1) "Engine exception" else "Engine exception ${index + 1}"65 println(TeamCityMessageBuilder.testStarted(prefix, testName).build())66 val message = error.message ?: t::class.bestName()67 println(TeamCityMessageBuilder.testFailed(prefix, testName).message(message).build())68 println(TeamCityMessageBuilder.testFinished(prefix, testName).build())69 }70 }71 }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
1package io.kotest.engine.teamcity2import 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
1package com.sksamuel.kotest.engine.teamcity2import io.kotest.core.source.SourceRef3import io.kotest.core.spec.style.FunSpec4import 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})...
locations
Using AI Code Generation
1val locations = Locations()2val test = Test()3val testStarted = TestStarted()4val testFailed = TestFailed()5val testIgnored = TestIgnored()6val testFinished = TestFinished()7val buildProblem = BuildProblem()8val buildStatus = BuildStatus()9val message = Message()10val blockOpened = BlockOpened()11val blockClosed = BlockClosed()12val testSuiteStarted = TestSuiteStarted()13val testSuiteFinished = TestSuiteFinished()14val flowId = FlowId()15val flowStarted = FlowStarted()16val flowFinished = FlowFinished()17val serviceMessage = ServiceMessage()18val build = Build()19val project = Project()20val testOccurrences = TestOccurrences()21val testOccurrence = TestOccurrence()
locations
Using AI Code Generation
1+import io.kotest.engine.teamcity.*2+class MySpec : StringSpec({3+ "my test" {4+ io.kotest.engine.teamcity.locations()5+ }6+})7+import io.kotest.engine.teamcity.*8+class MySpec : StringSpec({9+ "my test" {10+ io.kotest.engine.teamcity.sendMessage("myMessage")11+ }12+})13+import io.kotest.engine.teamcity.*14+class MySpec : StringSpec({15+ "my test" {16+ io.kotest.engine.teamcity.sendMessage("myMessage")17+ }18+})19+import io.kotest.engine.teamcity.*20+class MySpec : StringSpec({21+ "my test" {22+ io.kotest.engine.teamcity.sendTestMetadata("key", "value")23+ }24+})25+import io.kotest.engine.teamcity.*26+class MySpec : StringSpec({27+ "my test" {28+ io.kotest.engine.teamcity.sendTestMetadata("key", "value")29+ }30+})
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!!