How to use SpecInstantiationListener class of io.kotest.core.listeners package

Best Kotest code snippet using io.kotest.core.listeners.SpecInstantiationListener

SpecExtensions.kt

Source:SpecExtensions.kt Github

copy

Full Screen

...9import io.kotest.core.listeners.IgnoredSpecListener10import io.kotest.core.listeners.InstantiationErrorListener11import io.kotest.core.listeners.InstantiationListener12import io.kotest.core.listeners.PrepareSpecListener13import io.kotest.core.listeners.SpecInstantiationListener14import io.kotest.core.spec.Spec15import io.kotest.core.spec.functionOverrideCallbacks16import io.kotest.core.test.TestCase17import io.kotest.core.test.TestResult18import io.kotest.engine.extensions.ExtensionException19import io.kotest.engine.extensions.MultipleExceptions20import io.kotest.mpp.Logger21import io.kotest.mpp.bestName22import kotlin.reflect.KClass23/**24 * Used to invoke extension points / listeners / callbacks on specs.25 */26internal class SpecExtensions(private val registry: ExtensionRegistry) {27 private val logger = Logger(SpecExtensions::class)28 /**29 * Returns all [Extension]s applicable to a [Spec]. This includes extensions via30 * function overrides, those registered explicitly in the spec as part of the DSL,31 * and project wide extensions from configuration.32 */33 fun extensions(spec: Spec): List<Extension> {34 return spec.extensions() + // overriding the extensions function in the spec35 spec.listeners() + // overriding the listeners function in the spec36 spec.functionOverrideCallbacks() + // dsl37 spec.registeredExtensions() + // added to the spec via register38 registry.all() // globals39 }40 suspend fun beforeSpec(spec: Spec): Result<Spec> {41 logger.log { Pair(spec::class.bestName(), "beforeSpec $spec") }42 val errors = extensions(spec).filterIsInstance<BeforeSpecListener>().mapNotNull { ext ->43 runCatching { ext.beforeSpec(spec) }44 .mapError { ExtensionException.BeforeSpecException(it) }.exceptionOrNull()45 }46 return when {47 errors.isEmpty() -> Result.success(spec)48 errors.size == 1 -> Result.failure(errors.first())49 else -> Result.failure(MultipleExceptions(errors))50 }51 }52 /**53 * Runs all the after spec listeners for this [Spec]. All errors are caught and wrapped54 * in [AfterSpecListener] and if more than one error, all will be returned as a [MultipleExceptions].55 */56 suspend fun afterSpec(spec: Spec): Result<Spec> = runCatching {57 logger.log { Pair(spec::class.bestName(), "afterSpec $spec") }58 spec.registeredAutoCloseables().let { closeables ->59 logger.log { Pair(spec::class.bestName(), "Closing ${closeables.size} autocloseables [$closeables]") }60 closeables.forEach {61 if(it.isInitialized()) it.value.close() else Unit62 }63 }64 val errors = extensions(spec).filterIsInstance<AfterSpecListener>().mapNotNull { ext ->65 runCatching { ext.afterSpec(spec) }66 .mapError { ExtensionException.AfterSpecException(it) }.exceptionOrNull()67 }68 return when {69 errors.isEmpty() -> Result.success(spec)70 errors.size == 1 -> Result.failure(errors.first())71 else -> Result.failure(MultipleExceptions(errors))72 }73 }74 suspend fun specInstantiated(spec: Spec) = runCatching {75 logger.log { Pair(spec::class.bestName(), "specInstantiated $spec") }76 registry.all().filterIsInstance<SpecInstantiationListener>().forEach { it.specInstantiated(spec) }77 registry.all().filterIsInstance<InstantiationListener>().forEach { it.specInstantiated(spec) }78 }79 suspend fun specInstantiationError(kclass: KClass<out Spec>, t: Throwable) = runCatching {80 logger.log { Pair(kclass.bestName(), "specInstantiationError $t") }81 registry.all().filterIsInstance<SpecInstantiationListener>().forEach { it.specInstantiationError(kclass, t) }82 registry.all().filterIsInstance<InstantiationErrorListener>().forEach { it.instantiationError(kclass, t) }83 }84 suspend fun prepareSpec(kclass: KClass<out Spec>): Result<KClass<*>> {85 val exts = registry.all().filterIsInstance<PrepareSpecListener>()86 logger.log { Pair(kclass.bestName(), "prepareSpec (${exts.size})") }87 val errors = exts.mapNotNull {88 runCatching { it.prepareSpec(kclass) }89 .mapError { ExtensionException.PrepareSpecException(it) }.exceptionOrNull()90 }91 return when {92 errors.isEmpty() -> Result.success(kclass)93 errors.size == 1 -> Result.failure(errors.first())94 else -> Result.failure(MultipleExceptions(errors))95 }...

Full Screen

Full Screen

SpecWrapperExtension.kt

Source:SpecWrapperExtension.kt Github

copy

Full Screen

...12import io.kotest.core.listeners.FinalizeSpecListener13import io.kotest.core.listeners.IgnoredSpecListener14import io.kotest.core.listeners.InstantiationErrorListener15import io.kotest.core.listeners.PrepareSpecListener16import io.kotest.core.listeners.SpecInstantiationListener17import io.kotest.core.spec.Spec18import io.kotest.core.test.TestCase19import io.kotest.core.test.TestResult20import kotlin.reflect.KClass21/**22 * Wraps another extension, delegating spec extensions only for the specified spec.23 */24internal class SpecWrapperExtension(25 val delegate: Extension,26 val target: KClass<*>27) : SpecInstantiationListener,28 InstantiationErrorListener,29 SpecExtension,30 IgnoredSpecListener,31 AfterSpecListener,32 BeforeSpecListener,33 PrepareSpecListener,34 FinalizeSpecListener,35 BeforeTestListener,36 AfterTestListener,37 BeforeEachListener,38 AfterEachListener,39 BeforeContainerListener,40 AfterContainerListener {41 override suspend fun beforeContainer(testCase: TestCase) {42 if (delegate is BeforeContainerListener && testCase.spec::class == target) delegate.beforeContainer(testCase)43 }44 override suspend fun afterContainer(testCase: TestCase, result: TestResult) {45 if (delegate is AfterContainerListener && testCase.spec::class == target) delegate.afterContainer(46 testCase,47 result48 )49 }50 override suspend fun afterEach(testCase: TestCase, result: TestResult) {51 if (delegate is AfterEachListener && testCase.spec::class == target) delegate.afterEach(testCase, result)52 }53 override suspend fun afterAny(testCase: TestCase, result: TestResult) {54 if (delegate is AfterTestListener && testCase.spec::class == target) delegate.afterAny(testCase, result)55 }56 override suspend fun afterTest(testCase: TestCase, result: TestResult) {57 if (delegate is AfterTestListener && testCase.spec::class == target) delegate.afterTest(testCase, result)58 }59 override suspend fun beforeEach(testCase: TestCase) {60 if (delegate is BeforeEachListener && testCase.spec::class == target) delegate.beforeEach(testCase)61 }62 override suspend fun beforeAny(testCase: TestCase) {63 if (delegate is BeforeTestListener && testCase.spec::class == target) delegate.beforeAny(testCase)64 }65 override suspend fun beforeTest(testCase: TestCase) {66 if (delegate is BeforeTestListener && testCase.spec::class == target) delegate.beforeTest(testCase)67 }68 override suspend fun instantiationError(kclass: KClass<*>, t: Throwable) {69 if (delegate is InstantiationErrorListener && kclass == target) delegate.instantiationError(kclass, t)70 }71 override suspend fun ignoredSpec(kclass: KClass<*>, reason: String?) {72 if (delegate is IgnoredSpecListener && kclass == target) delegate.ignoredSpec(kclass, reason)73 }74 override suspend fun afterSpec(spec: Spec) {75 if (delegate is AfterSpecListener && spec::class == target) delegate.afterSpec(spec)76 }77 override suspend fun beforeSpec(spec: Spec) {78 if (delegate is BeforeSpecListener && spec::class == target) delegate.beforeSpec(spec)79 }80 override suspend fun finalizeSpec(kclass: KClass<out Spec>, results: Map<TestCase, TestResult>) {81 if (delegate is FinalizeSpecListener && kclass == target) delegate.finalizeSpec(kclass, results)82 }83 override fun specInstantiated(spec: Spec) {84 if (delegate is SpecInstantiationListener && spec::class == target) delegate.specInstantiated(spec)85 }86 override suspend fun intercept(spec: Spec, execute: suspend (Spec) -> Unit) {87 if (delegate is SpecExtension && spec::class == target) delegate.intercept(spec, execute) else execute(spec)88 }89 override suspend fun prepareSpec(kclass: KClass<out Spec>) {90 if (delegate is PrepareSpecListener && kclass == target) delegate.prepareSpec(kclass)91 }92 override suspend fun intercept(spec: KClass<out Spec>, process: suspend () -> Unit) {93 if (delegate is SpecExtension && spec == target) delegate.intercept(spec, process) else process()94 }95 override fun specInstantiationError(kclass: KClass<out Spec>, t: Throwable) {96 if (delegate is SpecInstantiationListener && kclass == target) delegate.specInstantiationError(kclass, t)97 }98}...

Full Screen

Full Screen

SpecInstantiationListenerTest.kt

Source:SpecInstantiationListenerTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.extensions.spec2import io.kotest.core.config.ProjectConfiguration3import io.kotest.core.listeners.SpecInstantiationListener4import io.kotest.core.annotation.Isolate5import io.kotest.core.spec.Spec6import io.kotest.core.spec.style.FunSpec7import io.kotest.engine.TestEngineLauncher8import io.kotest.engine.listener.NoopTestEngineListener9import io.kotest.matchers.shouldBe10import kotlin.reflect.KClass11@Isolate12class SpecInstantiationListenerTest : FunSpec() {13 init {14 test("SpecInstantiationListener.specInstantiated should be notified on success") {15 var fired = false16 val ext = object : SpecInstantiationListener {17 override fun specInstantiated(spec: Spec) {18 fired = true19 }20 override fun specInstantiationError(kclass: KClass<out Spec>, t: Throwable) {21 error("boom")22 }23 }24 val c = ProjectConfiguration()25 c.registry.add(ext)26 TestEngineLauncher(NoopTestEngineListener)27 .withClasses(SpecInstantiationSuccessSpec::class)28 .withConfiguration(c)29 .launch()30 fired shouldBe true31 }32 test("SpecInstantiationListener.specInstantiationError should be notified on failure") {33 var fired = false34 val ext = object : SpecInstantiationListener {35 override fun specInstantiated(spec: Spec) {36 error("boom")37 }38 override fun specInstantiationError(kclass: KClass<out Spec>, t: Throwable) {39 fired = true40 }41 }42 val c = ProjectConfiguration()43 c.registry.add(ext)44 TestEngineLauncher(NoopTestEngineListener)45 .withClasses(SpecInstantiationFailureSpec::class)46 .withConfiguration(c)47 .launch()48 fired shouldBe true...

Full Screen

Full Screen

SpecInstantiationListener.kt

Source:SpecInstantiationListener.kt Github

copy

Full Screen

1package io.kotest.core.listeners2import io.kotest.core.spec.Spec3import kotlin.reflect.KClass4@Deprecated("Deprecated in favour of InstantiationListener and InstantiationErrorListener which support suspension. Deprecated since 5.0.")5interface SpecInstantiationListener : Listener {6 fun specInstantiated(spec: Spec) {}7 fun specInstantiationError(kclass: KClass<out Spec>, t: Throwable) {}8}...

Full Screen

Full Screen

SpecInstantiationListener

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.listeners.SpecInstantiationListener2import io.kotest.core.spec.Spec3import io.kotest.core.spec.style.FunSpec4import io.kotest.matchers.shouldBe5import io.kotest.matchers.shouldNotBe6import kotlinx.coroutines.delay7import kotlinx.coroutines.runBlocking8class SpecInstantiationListenerExample : FunSpec({9 val listener = object : SpecInstantiationListener {10 override suspend fun beforeSpecInstantiation(spec: Spec) {11 println("Before Spec Instantiation")12 }13 override suspend fun afterSpecInstantiation(spec: Spec) {14 println("After Spec Instantiation")15 }16 }17 listeners(listener)18 test("test case") {19 runBlocking {20 delay(1000)21 }22 }23})24interface SpecInstantiationListener {25 suspend fun beforeSpecInstantiation(spec: Spec)26 suspend fun afterSpecInstantiation(spec: Spec)27}28import io.kotest.core.listeners.SpecInstantiationListener29import io.kotest.core.spec.Spec30import io.kotest.core.spec.style.FunSpec31import io.kotest.matchers.shouldBe32import io.kotest.matchers.shouldNotBe33import

Full Screen

Full Screen

SpecInstantiationListener

Using AI Code Generation

copy

Full Screen

1class MySpec : FunSpec() {2 override fun listeners() = listOf(SpecInstantiationListener())3}4class MySpec : FunSpec() {5 override fun listeners() = listOf(TestInstantiationListener())6}7class MySpec : FunSpec() {8 override fun listeners() = listOf(SpecInstantiationListener())9}10class MySpec : FunSpec() {11 override fun listeners() = listOf(TestInstantiationListener())12}13class MySpec : FunSpec() {14 override fun listeners() = listOf(SpecInstantiationListener())15}16class MySpec : FunSpec() {17 override fun listeners() = listOf(TestInstantiationListener())18}19class MySpec : FunSpec() {20 override fun listeners() = listOf(SpecInstantiationListener())21}22class MySpec : FunSpec() {23 override fun listeners() = listOf(TestInstantiationListener())24}25class MySpec : FunSpec() {26 override fun listeners() = listOf(SpecInstantiationListener())27}28class MySpec : FunSpec() {29 override fun listeners() = listOf(TestInstantiationListener())30}31class MySpec : FunSpec() {32 override fun listeners() = listOf(SpecInstantiationListener())33}34class MySpec : FunSpec() {35 override fun listeners() = listOf(TestInstantiationListener())36}37class MySpec : FunSpec() {38 override fun listeners() = listOf(SpecInstantiationListener())39}

Full Screen

Full Screen

SpecInstantiationListener

Using AI Code Generation

copy

Full Screen

1val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }2val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }3val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }4val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }5val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }6val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }7val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }8val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }9val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }10val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }11val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }12val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }13val listener = SpecInstantiationListener ( "MySpec" ) { MySpec ( ) }14val listener = SpecInstantiationListener ( "MySpec" ) { MySpec (

Full Screen

Full Screen

SpecInstantiationListener

Using AI Code Generation

copy

Full Screen

1val listener = SpecInstantiationListener { spec ->2println("Spec instantiated: $spec")3}4val listener = SpecInstantiationListener { spec ->5println("Spec instantiated: $spec")6}7import io.kotest.core.spec.style.FunSpec8import io.kotest.core.listeners.SpecInstantiationListener9import io.kotest.matchers.shouldBe10class MySpec : FunSpec() {11init {12listeners(SpecInstantiationListener { spec ->13println("Spec instantiated: $spec")14})15test("a test") {16}17}18}

Full Screen

Full Screen

SpecInstantiationListener

Using AI Code Generation

copy

Full Screen

1listeners(listOf(SpecInstantiationListener))2listeners(listOf(TestInstantiationListener))3listeners(listOf(SpecInstantiationListener))4listeners(listOf(TestInstantiationListener))5listeners(listOf(SpecInstantiationListener))6listeners(listOf(TestInstantiationListener))7listeners(listOf(SpecInstantiationListener))8listeners(listOf(TestInstantiationListener))9listeners(listOf(SpecInstantiationListener))10listeners(listOf(TestInstantiationListener))11listeners(listOf(SpecInstantiationListener))12listeners(listOf(TestInstantiationListener))13listeners(listOf(SpecInstantiationListener))

Full Screen

Full Screen

SpecInstantiationListener

Using AI Code Generation

copy

Full Screen

1@ Tag ( "once" )2class SpecInstantiationListenerTest : FunSpec ({3test ( "test 1" ) {4println ( "test 1" )5}6test ( "test 2" ) {7println ( "test 2" )8}9}) {10override fun listeners () = listOf (SpecInstantiationListener)11}12@ Tag ( "once" )13class SpecInstantiationListenerTest : FunSpec ({14test ( "test 1" ) {15println ( "test 1" )16}17test ( "test 2" ) {18println ( "test 2" )19}20}) {21override fun listeners () = listOf (SpecInstantiationListener)22}23@ Tag ( "once" )24class SpecInstantiationListenerTest : FunSpec ({25test ( "test 1" ) {26println ( "test 1" )27}28test ( "test 2" ) {29println ( "test 2" )30}31}) {32override fun listeners () = listOf (SpecInstantiationListener)33}34@ Tag ( "once" )35class SpecInstantiationListenerTest : FunSpec ({36test ( "test 1" ) {37println ( "test 1" )38}39test ( "test 2" ) {40println ( "test 2" )41}42}) {43override fun listeners () = listOf (SpecInstantiationListener)44}45@ Tag ( "once" )

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in SpecInstantiationListener

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful