Best Kotest code snippet using io.kotest.engine.errors.ExtensionExceptionExtractor.resolve
JUnitTestEngineListener.kt
Source:JUnitTestEngineListener.kt
...167 descriptors.clear()168 startedTests.clear()169 }170 private fun addPlaceholderTest(parent: TestDescriptor, t: Throwable, kclass: KClass<*>) {171 val (name, cause) = ExtensionExceptionExtractor.resolve(t)172 val descriptor = createTestDescriptor(173 parent.uniqueId.append(Segment.Test.value, name),174 name,175 TestDescriptor.Type.TEST,176 ClassSource.from(kclass.java),177 false178 )179 parent.addChild(descriptor)180 listener.dynamicTestRegistered(descriptor)181 listener.executionStarted(descriptor)182 listener.executionFinished(descriptor, TestResult.Error(Duration.ZERO, cause).toTestExecutionResult())183 }184 override suspend fun testStarted(testCase: TestCase) {185 // depending on the test type, we may want to wait to notify junit, this is because gradle doesn't work186 // properly with the junit test types. Ideally, we'd just set everything to CONTAINER_AND_TEST, which is187 // supposed to mean a test can contain other tests as well as being a test itself, which is exactly how188 // Kotest views tests, but unfortunately it doesn't work properly.189 //190 // Another approach is to wait until the spec finishes to see which tests contain children and which191 // don't and set the test type appropriately, but junit doesn't give us a way to specify test duration192 // (instead it just calculates it itself from the time between marking a test as started and marking193 // it as finished), so this approach works but ends up having all tests as 0ms194 //195 // So the approach we will take is use the TestType from the test definition, unless it is dynamic,196 // then for dynamic we will calculate it later, and accept the 0ms drawback197 logger.log { Pair(testCase.name.testName, "test started") }198 if (testCase.parent != null) rootTests.add(testCase)199 addChild(testCase)200 when (testCase.type) {201 TestType.Container -> startTestIfNotStarted(testCase, TestDescriptor.Type.CONTAINER)202 TestType.Test -> startTestIfNotStarted(testCase, TestDescriptor.Type.TEST)203 TestType.Dynamic -> Unit204 }205 }206 // this test can be output now it has completed as we have all we need to know to complete it207 override suspend fun testFinished(testCase: TestCase, result: TestResult) {208 logger.log { Pair(testCase.name.testName, "test finished $result") }209 results[testCase.descriptor] = result210 val descriptor = getOrCreateTestDescriptor(testCase, null)211 // we need to ensure all parents have been started first212 startParents(testCase)213 startTestIfNotStarted(testCase, null)214 logger.log { Pair(testCase.name.testName, "executionFinished: $descriptor") }215 listener.executionFinished(descriptor, result.toTestExecutionResult())216 }217 override suspend fun testIgnored(testCase: TestCase, reason: String?) {218 logger.log { Pair(testCase.name.testName, "test ignored $reason") }219 if (testCase.parent == null) rootTests.add(testCase)220 addChild(testCase)221 results[testCase.descriptor] = TestResult.Ignored(reason)222 // we need to ensure all parents have been started first223 startParents(testCase)224 val descriptor = getOrCreateTestDescriptor(testCase, TestDescriptor.Type.TEST)225 logger.log { Pair(testCase.name.testName, "Registering dynamic test: $descriptor") }226 listener.dynamicTestRegistered(descriptor)227 logger.log { Pair(testCase.name.testName, "executionSkipped: $descriptor") }228 listener.executionSkipped(descriptor, reason)229 }230 private fun addChild(testCase: TestCase) {231 children.getOrPut(testCase.descriptor.parent) { mutableListOf() }.add(testCase)232 }233 private fun startParents(testCase: TestCase) {234 val parent = testCase.parent235 if (parent != null) {236 startParents(parent)237 startTestIfNotStarted(parent, null)238 }239 }240 private fun startTestIfNotStarted(testCase: TestCase, type: TestDescriptor.Type?) {241 if (!startedTests.contains(testCase.descriptor)) {242 val descriptor = getOrCreateTestDescriptor(testCase, type)243 logger.log { Pair(testCase.name.testName, "Registering dynamic test: $descriptor") }244 listener.dynamicTestRegistered(descriptor)245 logger.log { Pair(testCase.name.testName, "executionStarted: $descriptor") }246 listener.executionStarted(descriptor)247 startedTests.add(testCase.descriptor)248 }249 }250 private fun getOrCreateTestDescriptor(testCase: TestCase, type: TestDescriptor.Type?): TestDescriptor {251 val existing = descriptors[testCase.descriptor]252 if (existing != null) return existing253 val parent = when (val p = testCase.parent) {254 null -> getSpecDescriptor(testCase.spec::class)255 else -> getOrCreateTestDescriptor(p, null)256 }257 val id = parent.uniqueId.append(testCase.descriptor)258 // we dynamically work out the type if null by looking to see if this test had any children259 val c = children[testCase.descriptor]260 val t = when {261 type != null -> type262 c == null || c.isEmpty() -> TestDescriptor.Type.TEST263 else -> TestDescriptor.Type.CONTAINER264 }265 return createTestDescriptor(266 id,267 formatter.format(testCase),268 t,269 ClassSource.from(testCase.spec::class.java, null), // gradle-junit-platform hides tests if we don't send this270 type == TestDescriptor.Type.CONTAINER271 ).apply {272 parent.addChild(this)273 descriptors[testCase.descriptor] = this274 }275 }276 private fun getSpecDescriptor(kclass: KClass<*>): TestDescriptor {277 return getSpecDescriptor(root, kclass.toDescriptor(), formatter.format(kclass))278 }279 private fun createAndRegisterDummySpec(name: String): TestDescriptor {280 val unique = UniqueNames.unique(name, dummies) { s, k -> "${s}_$k" } ?: name281 dummies.add(unique)282 val descriptor = getSpecDescriptor(root, Descriptor.SpecDescriptor(DescriptorId(unique), this::class), unique)283 listener.dynamicTestRegistered(descriptor)284 return descriptor285 }286 private fun registerExceptionPlaceholders(ts: List<Throwable>) {287 ts.forEach {288 val (name, cause) = ExtensionExceptionExtractor.resolve(it)289 val container = createAndRegisterDummySpec(name)290 listener.executionStarted(container)291 listener.executionFinished(container, TestExecutionResult.failed(cause))292 }293 }294}...
TeamCityTestEngineListener.kt
Source:TeamCityTestEngineListener.kt
...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()...
ExtensionExceptionExtractor.kt
Source:ExtensionExceptionExtractor.kt
...11 * callback was to fail, we cannot display the failure. The workaround is to add a dummy test12 * to the test suite, with some name like 'afterSpecFailure' and attach the exception there.13 */14object ExtensionExceptionExtractor {15 fun resolve(t: Throwable): Pair<String, Throwable> {16 val cause = t.cause ?: t17 val name = when (t) {18 is ExtensionException -> when (t) {19 is ExtensionException.BeforeProjectException -> "Before Project Error"20 is ExtensionException.AfterProjectException -> "After Project Error"21 is ExtensionException.BeforeTestException -> "Before Test Error"22 is ExtensionException.AfterTestException -> "After Test Error"23 is ExtensionException.BeforeEachException -> "Before Each Error"24 is ExtensionException.AfterEachException -> "After Each Error"25 is ExtensionException.BeforeContainerException -> "Before Container Error"26 is ExtensionException.AfterContainerException -> "After Container Error"27 is ExtensionException.BeforeAnyException -> "Before Any Error"28 is ExtensionException.AfterAnyException -> "After Any Error"29 is ExtensionException.BeforeInvocationException -> "Before Invocation Error"...
resolve
Using AI Code Generation
1val extensionExceptionExtractor = ExtensionExceptionExtractor()2val extensionException = extensionExceptionExtractor.resolve(throwable)3val testEngineExceptionExtractor = TestEngineExceptionExtractor()4val testEngineException = testEngineExceptionExtractor.resolve(throwable)5val specExceptionExtractor = SpecExceptionExtractor()6val specException = specExceptionExtractor.resolve(throwable)7val testCaseExceptionExtractor = TestCaseExceptionExtractor()8val testCaseException = testCaseExceptionExtractor.resolve(throwable)9val testEngineListenerExceptionExtractor = TestEngineListenerExceptionExtractor()10val testEngineListenerException = testEngineListenerExceptionExtractor.resolve(throwable)11val testEngineListenerExceptionExtractor = TestEngineListenerExceptionExtractor()12val testEngineListenerException = testEngineListenerExceptionExtractor.resolve(throwable)13val testCaseExtensionExceptionExtractor = TestCaseExtensionExceptionExtractor()14val testCaseExtensionException = testCaseExtensionExceptionExtractor.resolve(throwable)15val specExtensionExceptionExtractor = SpecExtensionExceptionExtractor()16val specExtensionException = specExtensionExceptionExtractor.resolve(throwable)17val specExtensionExceptionExtractor = SpecExtensionExceptionExtractor()18val specExtensionException = specExtensionExceptionExtractor.resolve(throwable)19val specExtensionExceptionExtractor = SpecExtensionExceptionExtractor()20val specExtensionException = specExtensionExceptionExtractor.resolve(throwable)21val specExtensionExceptionExtractor = SpecExtensionExceptionExtractor()22val specExtensionException = specExtensionExceptionExtractor.resolve(throwable)23val specExtensionExceptionExtractor = SpecExtensionExceptionExtractor()
resolve
Using AI Code Generation
1val exceptionExtractor = ExtensionExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)2val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)3val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)4val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)5val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)6val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)7val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)8val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)9val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)10val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)11val exceptionExtractor = JUnitExceptionExtractor ( this .javaClass.classLoader) val exception = exceptionExtractor.resolve(throwable)
resolve
Using AI Code Generation
1val cause = ExtensionExceptionExtractor.resolve(ex)2println(cause)3at io.kotest.core.test.TestExecutor$execute$3.invokeSuspend(TestExecutor.kt:99)4at io.kotest.core.test.TestExecutor$execute$3.invoke(TestExecutor.kt)5at io.kotest.core.test.TestExecutor$execute$3.invoke(TestExecutor.kt)6at io.kotest.core.test.TestExecutor$execute$1.invokeSuspend(TestExecutor.kt:73)7at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)8at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)9at io.kotest.core.test.TestExecutor$execute$1.invokeSuspend(TestExecutor.kt:73)10at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)11at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)12at io.kotest.core.test.TestExecutor$execute$1.invokeSuspend(TestExecutor.kt:73)13at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)14at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)15at io.kotest.core.test.TestExecutor$execute$1.invokeSuspend(TestExecutor.kt:73)16at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)17at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)18at io.kotest.core.test.TestExecutor$execute$1.invokeSuspend(TestExecutor.kt:73)19at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)20at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)21at io.kotest.core.test.TestExecutor$execute$1.invokeSuspend(TestExecutor.kt:73)22at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)23at io.kotest.core.test.TestExecutor$execute$1.invoke(TestExecutor.kt)24at io.kotest.core.test.TestExecutor$execute$1.invokeSuspend(TestExecutor.kt:73)
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!!