Best Kotest code snippet using io.kotest.engine.interceptors.EngineContext.mergeListener
SpecInitializationErrorTest.kt
Source:SpecInitializationErrorTest.kt
...34 override fun executionStarted(testDescriptor: TestDescriptor?) {}35 override fun dynamicTestRegistered(testDescriptor: TestDescriptor?) {}36 }37 val listener = JUnitTestEngineListener(engineListener, root)38 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).mergeListener(listener))39 executor.execute(SpecRef.Reference(SpecWithFieldError::class))40 finished.toMap() shouldBe mapOf(41 "SpecInstantiationException" to TestExecutionResult.Status.FAILED,42 "com.sksamuel.kotest.runner.junit5.SpecWithFieldError" to TestExecutionResult.Status.FAILED43 )44 }45 test("an error in a class initializer should fail spec") {46 val root = KotestEngineDescriptor(47 UniqueId.forEngine("kotest"),48 emptyList(),49 emptyList(),50 emptyList(),51 null,52 )53 val finished = mutableMapOf<String, TestExecutionResult.Status>()54 val engineListener = object : EngineExecutionListener {55 override fun executionFinished(testDescriptor: TestDescriptor, testExecutionResult: TestExecutionResult) {56 finished[testDescriptor.displayName] = testExecutionResult.status57 }58 override fun reportingEntryPublished(testDescriptor: TestDescriptor?, entry: ReportEntry?) {}59 override fun executionSkipped(testDescriptor: TestDescriptor?, reason: String?) {}60 override fun executionStarted(testDescriptor: TestDescriptor?) {}61 override fun dynamicTestRegistered(testDescriptor: TestDescriptor?) {}62 }63 val listener = JUnitTestEngineListener(engineListener, root)64 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).mergeListener(listener))65 executor.execute(SpecRef.Reference(SpecWithInitError::class))66 finished.toMap() shouldBe mapOf(67 "SpecInstantiationException" to TestExecutionResult.Status.FAILED,68 "com.sksamuel.kotest.runner.junit5.SpecWithInitError" to TestExecutionResult.Status.FAILED,69 )70 }71})72private class SpecWithFieldError : FunSpec() {73 private val err = "failme".apply { error("foo") }74 init {75 test("foo") {76 }77 }78}...
ConcurrentTestSuiteScheduler.kt
Source:ConcurrentTestSuiteScheduler.kt
...55 if (context.configuration.projectWideFailFast && collector.errors) {56 context.listener.specIgnored(ref.kclass, null)57 } else {58 try {59 val executor = SpecExecutor(coroutineDispatcherFactory, context.mergeListener(collector))60 logger.log { Pair(ref.kclass.bestName(), "Executing ref") }61 executor.execute(ref)62 } catch (t: Throwable) {63 logger.log { Pair(ref.kclass.bestName(), "Unhandled error during spec execution $t") }64 throw t65 }66 }67 }68 logger.log { Pair(ref.kclass.bestName(), "Released permit") }69 }70 }71 }72}...
EngineInterceptor.kt
Source:EngineInterceptor.kt
...35 }36 /**37 * Returns this [EngineContext] with the given [listener] added via a [CompositeTestEngineListener].38 */39 fun mergeListener(listener: TestEngineListener): EngineContext {40 return EngineContext(suite, CompositeTestEngineListener(listOf(this.listener, listener)), tags, configuration)41 }42 fun withTestSuite(suite: TestSuite): EngineContext {43 return EngineContext(suite, listener, tags, configuration)44 }45 fun withListener(listener: TestEngineListener): EngineContext {46 return EngineContext(suite, listener, tags, configuration)47 }48 fun withConfiguration(c: ProjectConfiguration): EngineContext {49 return EngineContext(suite, listener, tags, c)50 }51 fun withTags(tags: TagExpression): EngineContext {52 return EngineContext(suite, listener, tags, configuration)53 }...
TestEngineStartedFinishedInterceptorTest.kt
Source:TestEngineStartedFinishedInterceptorTest.kt
...16 fired += "a"17 }18 }19 TestEngineStartedFinishedInterceptor.intercept(20 EngineContext.empty.mergeListener(listener)21 ) {22 fired += "b"23 EngineResult.empty24 }25 fired.shouldBe("ab")26 }27 test("should invoke engineFinished after downstream") {28 var fired = ""29 val listener = object : AbstractTestEngineListener() {30 override suspend fun engineFinished(t: List<Throwable>) {31 fired += "a"32 }33 }34 TestEngineStartedFinishedInterceptor.intercept(35 EngineContext.empty.mergeListener(listener)36 ) {37 fired += "b"38 EngineResult(listOf(Exception("foo")))39 }40 fired.shouldBe("ba")41 }42 test("should invoke engineFinished with errors") {43 var errors = emptyList<Throwable>()44 val listener = object : AbstractTestEngineListener() {45 override suspend fun engineFinished(t: List<Throwable>) {46 errors = t47 }48 }49 TestEngineStartedFinishedInterceptor.intercept(50 EngineContext.empty.mergeListener(listener)51 ) { EngineResult(listOf(Exception("foo"))) }52 errors.shouldHaveSize(1)53 }54})...
TestSuiteScheduler.kt
Source:TestSuiteScheduler.kt
...33 try {34 if (context.configuration.projectWideFailFast && collector.errors) {35 context.listener.specIgnored(it.kclass, null)36 } else {37 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, context.mergeListener(collector))38 executor.execute(it)39 }40 } catch (e:Throwable) {41 println(e)42 errors.add(e)43 }44 }45 return EngineResult(errors.toList())46 }47}...
WriteFailuresInterceptor.kt
Source:WriteFailuresInterceptor.kt
...21 execute: suspend (EngineContext) -> EngineResult22 ): EngineResult {23 return if (context.configuration.writeSpecFailureFile) {24 val collector = CollectingTestEngineListener()25 val result = execute(context.mergeListener(collector))26 val failedSpecs = collector.tests27 .filterValues { it.isErrorOrFailure }28 .map { it.key.spec::class }29 .toSet()30 writeSpecFailures(failedSpecs, context.configuration.specFailureFilePath)31 result32 } else {33 execute(context)34 }35 }36 private fun writeSpecFailures(failures: Set<KClass<out Spec>>, filename: String) {37 val path = Paths.get(filename).toAbsolutePath()38 path.parent.toFile().mkdirs()39 val content = failures.distinct().joinToString("\n") { it.java.canonicalName }...
EmptyTestSuiteInterceptor.kt
Source:EmptyTestSuiteInterceptor.kt
...15 ): EngineResult {16 return when (context.configuration.failOnEmptyTestSuite) {17 true -> {18 val collector = CollectingTestEngineListener()19 val result = execute(context.mergeListener(collector))20 when {21 collector.tests.isEmpty() -> EngineResult(result.errors + EmptyTestSuiteException)22 else -> result23 }24 }25 false -> execute(context)26 }27 }28}29/**30 * Exception used to indicate that the engine had no specs to execute.31 */32object EmptyTestSuiteException : Exception("No specs were available to test")...
TestEngineInitializeInterceptorTest.kt
Source:TestEngineInitializeInterceptorTest.kt
...13 fired = true14 }15 }16 TestEngineInitializedInterceptor.intercept(17 EngineContext.empty.mergeListener(listener)18 ) { EngineResult.empty }19 fired.shouldBeTrue()20 }21})...
mergeListener
Using AI Code Generation
1fun EngineContext . mergeListener ( listener : TestEngineListener ) {2this . listener = object : TestEngineListener {3override fun engineStarted ( specs : List < Spec > ) {4listener . engineStarted ( specs )5this .@ mergeListener . listener . engineStarted ( specs )6}7override fun engineFinished ( t : List < Throwable > ) {8listener . engineFinished ( t )9this .@ mergeListener . listener . engineFinished ( t )10}11override fun specStarted ( spec : Spec ) {12listener . specStarted ( spec )13this .@ mergeListener . listener . specStarted ( spec )14}15override fun specFinished ( spec : Spec , results : Map < TestCase , TestResult > ) {16listener . specFinished ( spec , results )17this .@ mergeListener . listener . specFinished ( spec , results )18}19override fun testStarted ( testCase : TestCase ) {20listener . testStarted ( testCase )21this .@ mergeListener . listener . testStarted ( testCase )22}23override fun testFinished ( testCase : TestCase , result : TestResult ) {24listener . testFinished ( testCase , result )25this .@ mergeListener . listener . testFinished ( testCase , result )26}27}28}29}30val engineContext = EngineContext ( listener , specs , configuration , coroutineDispatcherFactory )31engineContext . mergeListener ( object : TestEngineListener {32override fun engineStarted ( specs : List < Spec > ) {33println ( "Engine started" )34}35override fun engineFinished ( t : List < Throwable > ) {36println ( "Engine finished" )37}38override fun specStarted ( spec : Spec ) {39println ( "Spec started" )40}41override fun specFinished ( spec : Spec , results : Map < TestCase , TestResult > ) {42println ( "Spec finished" )43}44override fun testStarted ( testCase : TestCase ) {45println ( "Test started" )46}47override fun testFinished ( testCase : TestCase , result : TestResult ) {48println ( "Test finished" )
mergeListener
Using AI Code Generation
1val engineContext = EngineContext ( engineListener , engineContext . configuration ) val engineListener = engineContext . mergeListener ( myCustomListener ) val engineContext = EngineContext ( engineListener , engineContext . configuration )2val specContext = SpecContext ( spec , specListener , specContext . configuration ) val specListener = specContext . mergeListener ( myCustomListener ) val specContext = SpecContext ( spec , specListener , specContext . configuration )3val testCaseContext = TestCaseContext ( testCase , testCaseListener , testCaseContext . configuration ) val testCaseListener = testCaseContext . mergeListener ( myCustomListener ) val testCaseContext = TestCaseContext ( testCase , testCaseListener , testCaseContext . configuration )4val testContext = TestContext ( test , testListener , testContext . configuration ) val testListener = testContext . mergeListener ( myCustomListener ) val testContext = TestContext ( test , testListener , testContext . configuration )5val testScopeContext = TestScopeContext ( testScope , testScopeListener , testScopeContext . configuration ) val testScopeListener = testScopeContext . mergeListener ( myCustomListener ) val testScopeContext = TestScopeContext ( testScope , testScopeListener , testScopeContext . configuration )6val testContext = TestContext ( test , testListener , testContext . configuration ) val testListener = testContext . mergeListener ( myCustomListener ) val testContext = TestContext ( test , testListener , testContext . configuration )7val testScopeContext = TestScopeContext ( testScope , testScopeListener , testScopeContext . configuration ) val testScopeListener = testScopeContext . mergeListener ( myCustomListener ) val testScopeContext = TestScopeContext ( testScope , testScopeListener , testScopeContext . configuration )8val testContext = TestContext (
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!!