How to use Spec.functionOverrideCallbacks method of io.kotest.core.spec.listeners class

Best Kotest code snippet using io.kotest.core.spec.listeners.Spec.functionOverrideCallbacks

SpecExtensions.kt

Source:SpecExtensions.kt Github

copy

Full Screen

1package io.kotest.engine.spec2import io.kotest.common.mapError3import io.kotest.core.config.ExtensionRegistry4import io.kotest.core.extensions.Extension5import io.kotest.core.extensions.SpecExtension6import io.kotest.core.listeners.AfterSpecListener7import io.kotest.core.listeners.BeforeSpecListener8import io.kotest.core.listeners.FinalizeSpecListener9import 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 }96 }97 suspend fun finalizeSpec(98 kclass: KClass<out Spec>,99 results: Map<TestCase, TestResult>,100 t: Throwable?101 ): Result<KClass<out Spec>> {102 val exts = registry.all().filterIsInstance<FinalizeSpecListener>()103 logger.log { Pair(kclass.bestName(), "finishSpec (${exts.size}) results:$results") }104 val errors = exts.mapNotNull {105 runCatching { it.finalizeSpec(kclass, results) }106 .mapError { ExtensionException.FinalizeSpecException(it) }.exceptionOrNull()107 }108 return when {109 errors.isEmpty() -> Result.success(kclass)110 errors.size == 1 -> Result.failure(errors.first())111 else -> Result.failure(MultipleExceptions(errors))112 }113 }114 suspend fun <T> intercept(spec: Spec, f: suspend () -> T): T? {115 val exts = extensions(spec).filterIsInstance<SpecExtension>()116 logger.log { Pair(spec::class.bestName(), "Intercepting spec with ${exts.size} spec extensions") }117 var result: T? = null118 val initial: suspend () -> Unit = {119 result = f()120 }121 val chain = exts.foldRight(initial) { op, acc ->122 {123 op.intercept(spec::class) {124 op.intercept(spec) {125 acc()126 }127 }128 }129 }130 chain.invoke()131 return result132 }133 /**134 * Notify all [IgnoredSpecListener]s that the given [kclass] has been ignored.135 */136 suspend fun ignored(kclass: KClass<out Spec>, reason: String?): Result<KClass<out Spec>> {137 val exts = registry.all().filterIsInstance<IgnoredSpecListener>()138 logger.log { Pair(kclass.bestName(), "ignored ${exts.size} extensions on $kclass") }139 val errors = exts.mapNotNull {140 runCatching { it.ignoredSpec(kclass, reason) }141 .mapError { ExtensionException.IgnoredSpecException(it) }.exceptionOrNull()142 }143 return when {144 errors.isEmpty() -> Result.success(kclass)145 errors.size == 1 -> Result.failure(errors.first())146 else -> Result.failure(MultipleExceptions(errors))147 }148 }149}...

Full Screen

Full Screen

TestExtensions.kt

Source:TestExtensions.kt Github

copy

Full Screen

1package io.kotest.engine.test2import io.kotest.common.collect3import io.kotest.common.mapError4import io.kotest.core.config.ExtensionRegistry5import io.kotest.core.extensions.Extension6import io.kotest.core.extensions.TestCaseExtension7import io.kotest.core.listeners.AfterContainerListener8import io.kotest.core.listeners.AfterEachListener9import io.kotest.core.listeners.AfterInvocationListener10import io.kotest.core.listeners.AfterTestListener11import io.kotest.core.listeners.BeforeContainerListener12import io.kotest.core.listeners.BeforeEachListener13import io.kotest.core.listeners.BeforeInvocationListener14import io.kotest.core.listeners.BeforeTestListener15import io.kotest.core.spec.functionOverrideCallbacks16import io.kotest.core.test.TestCase17import io.kotest.core.test.TestResult18import io.kotest.core.test.TestScope19import io.kotest.core.test.TestType20import io.kotest.engine.extensions.ExtensionException21import io.kotest.engine.extensions.MultipleExceptions22import io.kotest.engine.test.logging.LogExtension23import io.kotest.engine.test.scopes.withCoroutineContext24import kotlin.coroutines.coroutineContext25/**26 * Used to invoke extension points on tests.27 */28internal class TestExtensions(private val registry: ExtensionRegistry) {29 /**30 * Returns all [Extension]s applicable to a [TestCase]. This includes extensions31 * included in test case config, those at the spec level, and project wide from32 * the registry.33 */34 fun extensions(testCase: TestCase): List<Extension> {35 return testCase.config.extensions +36 testCase.spec.extensions() + // overriding the extensions function in the spec37 testCase.spec.listeners() + // overriding the listeners function in the spec38 testCase.spec.functionOverrideCallbacks() + // spec level dsl eg beforeTest { }39 testCase.spec.registeredExtensions() + // added to the spec via register40 registry.all() // globals41 }42 suspend fun beforeInvocation(testCase: TestCase, invocation: Int): Result<TestCase> {43 val extensions = extensions(testCase).filterIsInstance<BeforeInvocationListener>()44 return extensions.map {45 runCatching {46 it.beforeInvocation(testCase, invocation)47 }.mapError { ExtensionException.BeforeInvocationException(it) }48 }.collect { if (it.size == 1) it.first() else MultipleExceptions(it) }.map { testCase }49 }50 suspend fun afterInvocation(testCase: TestCase, invocation: Int): Result<TestCase> {51 val extensions = extensions(testCase).filterIsInstance<AfterInvocationListener>()52 return extensions.map {53 runCatching {54 it.afterInvocation(testCase, invocation)55 }.mapError { ExtensionException.AfterInvocationException(it) }56 }.collect { if (it.size == 1) it.first() else MultipleExceptions(it) }.map { testCase }57 }58 /**59 * Invokes all beforeXYZ callbacks for this test, checking for the appropriate test type.60 * Returns a Result of [MultipleExceptions] if there are any exceptions.61 */62 suspend fun beforeTestBeforeAnyBeforeContainer(testCase: TestCase): Result<TestCase> {63 val bt = extensions(testCase).filterIsInstance<BeforeTestListener>()64 val bc = extensions(testCase).filterIsInstance<BeforeContainerListener>()65 val be = extensions(testCase).filterIsInstance<BeforeEachListener>()66 val errors = bc.mapNotNull {67 runCatching {68 if (testCase.type == TestType.Container) it.beforeContainer(testCase)69 }.mapError { ExtensionException.BeforeContainerException(it) }.exceptionOrNull()70 } + be.mapNotNull {71 runCatching {72 if (testCase.type == TestType.Test) it.beforeEach(testCase)73 }.mapError { ExtensionException.BeforeEachException(it) }.exceptionOrNull()74 } + bt.mapNotNull {75 runCatching {76 it.beforeAny(testCase)77 }.mapError { ExtensionException.BeforeAnyException(it) }.exceptionOrNull()78 } + bt.mapNotNull {79 runCatching {80 it.beforeTest(testCase)81 }.mapError { ExtensionException.BeforeTestException(it) }.exceptionOrNull()82 }83 return when {84 errors.isEmpty() -> Result.success(testCase)85 errors.size == 1 -> Result.failure(errors.first())86 else -> Result.failure(MultipleExceptions(errors))87 }88 }89 /**90 * Invokes all beforeXYZ callbacks for this test.91 * Returns a Result of [MultipleExceptions] if there are any exceptions.92 */93 suspend fun afterTestAfterAnyAfterContainer(testCase: TestCase, result: TestResult): Result<TestResult> {94 val at = extensions(testCase).filterIsInstance<AfterTestListener>()95 val ac = extensions(testCase).filterIsInstance<AfterContainerListener>()96 val ae = extensions(testCase).filterIsInstance<AfterEachListener>()97 val errors = at.mapNotNull {98 runCatching {99 it.afterTest(testCase, result)100 }.mapError { ExtensionException.AfterTestException(it) }.exceptionOrNull()101 } + at.mapNotNull {102 runCatching {103 it.afterAny(testCase, result)104 }.mapError { ExtensionException.AfterAnyException(it) }.exceptionOrNull()105 } + ac.mapNotNull {106 runCatching {107 if (testCase.type == TestType.Container) it.afterContainer(testCase, result)108 }.mapError { ExtensionException.AfterContainerException(it) }.exceptionOrNull()109 } + ae.mapNotNull {110 runCatching {111 if (testCase.type == TestType.Test) it.afterEach(testCase, result)112 }.mapError { ExtensionException.AfterEachException(it) }.exceptionOrNull()113 }114 return when {115 errors.isEmpty() -> Result.success(result)116 errors.size == 1 -> Result.failure(errors.first())117 else -> Result.failure(MultipleExceptions(errors))118 }119 }120 /**121 * Executes the [TestCaseExtension]s for this [TestCase].122 */123 suspend fun intercept(124 testCase: TestCase,125 context: TestScope,126 inner: suspend (TestCase, TestScope) -> TestResult,127 ): TestResult {128 val execute = extensions(testCase).filterIsInstance<TestCaseExtension>()129 .foldRight(inner) { extension, execute ->130 { tc, ctx ->131 extension.intercept(tc) {132 // the user's intercept method is free to change the context of the coroutine133 // to support this, we should switch the context used by the test case context134 execute(it, ctx.withCoroutineContext(coroutineContext))135 }136 }137 }138 return execute(testCase, context)139 }140 fun logExtensions(testCase: TestCase): List<LogExtension> {141 return extensions(testCase).filterIsInstance<LogExtension>()142 }143}...

Full Screen

Full Screen

listeners.kt

Source:listeners.kt Github

copy

Full Screen

1package io.kotest.core.spec2import io.kotest.core.listeners.TestListener3import io.kotest.core.test.TestCase4import io.kotest.core.test.TestResult5/**6 * Returns a [TestListener] which passes lifecycle events through to the appropriate7 * spec member function.8 */9fun Spec.functionOverrideCallbacks() = object : TestListener {10 override suspend fun afterSpec(spec: Spec) {11 this@functionOverrideCallbacks.afterSpec(spec)12 }13 override suspend fun beforeSpec(spec: Spec) {14 this@functionOverrideCallbacks.beforeSpec(spec)15 }16 override suspend fun afterTest(testCase: TestCase, result: TestResult) {17 this@functionOverrideCallbacks.afterTest(testCase, result)18 }19 override suspend fun beforeTest(testCase: TestCase) {20 this@functionOverrideCallbacks.beforeTest(testCase)21 }22 override suspend fun afterContainer(testCase: TestCase, result: TestResult) {23 this@functionOverrideCallbacks.afterContainer(testCase, result)24 }25 override suspend fun beforeContainer(testCase: TestCase) {26 this@functionOverrideCallbacks.beforeContainer(testCase)27 }28 override suspend fun afterEach(testCase: TestCase, result: TestResult) {29 this@functionOverrideCallbacks.afterEach(testCase, result)30 }31 override suspend fun beforeEach(testCase: TestCase) {32 this@functionOverrideCallbacks.beforeEach(testCase)33 }34 override suspend fun afterAny(testCase: TestCase, result: TestResult) {35 this@functionOverrideCallbacks.afterAny(testCase, result)36 }37 override suspend fun beforeAny(testCase: TestCase) {38 this@functionOverrideCallbacks.beforeAny(testCase)39 }40}...

Full Screen

Full Screen

Spec.functionOverrideCallbacks

Using AI Code Generation

copy

Full Screen

1Spec.functionOverrideCallbacks(2beforeSpec = {3println("beforeSpec")4},5afterSpec = {6println("afterSpec")7},8beforeTest = {9println("beforeTest")10},11afterTest = {12println("afterTest")13}14}15}

Full Screen

Full Screen

Spec.functionOverrideCallbacks

Using AI Code Generation

copy

Full Screen

1class MySpec : FunSpec() {2override fun listeners() = listOf(3object : SpecFunctionOverrideListener {4override fun beforeSpecClass(spec: Spec, tests: List<Test>) {5println("Before Spec Class")6}7override fun afterSpecClass(spec: Spec, results: Map<Test, TestResult>) {8println("After Spec Class")9}10override fun beforeSpec(spec: Spec) {11println("Before Spec")12}13override fun afterSpec(spec: Spec) {14println("After Spec")15}16override fun beforeTest(testCase: TestCase) {17println("Before Test")18}19override fun afterTest(testCase: TestCase, result: TestResult) {20println("After Test")21}22override fun beforeContainer(testCase: TestCase) {23println("Before Container")24}25override fun afterContainer(testCase: TestCase, result: TestResult) {26println("After Container")27}28override fun beforeAny(testCase: TestCase) {29println("Before Any")30}31override fun afterAny(testCase: TestCase, result: TestResult) {32println("After Any")33}34override fun beforeTestSuite() {35println("Before Test Suite")36}37override fun afterTestSuite() {38println("After Test Suite")39}40}41}42}43class MySpec : FunSpec({44functionOverrideCallbacks(45beforeSpecClass = { spec, tests -> println("Before Spec Class") },46afterSpecClass = { spec, results -> println("After Spec Class") },47beforeSpec = { spec -> println("Before Spec") },48afterSpec = { spec -> println("After Spec") },49beforeTest = { testCase -> println("Before Test") },50afterTest = { testCase, result -> println("After Test") },51beforeContainer = { testCase -> println("Before Container") },52afterContainer = { testCase, result -> println("After Container") },53beforeAny = { testCase -> println("Before Any") },54afterAny = { testCase, result -> println("After Any") },55beforeTestSuite = { println("Before Test Suite") },56afterTestSuite = { println("After Test Suite") }57})58class MySpec : FunSpec({59functionOverrideCallbacks(60beforeSpecClass = { spec, tests -> println("Before Spec Class") },61afterSpecClass = { spec, results -> println("After Spec Class") },62beforeSpec = { spec -> println("Before

Full Screen

Full Screen

Spec.functionOverrideCallbacks

Using AI Code Generation

copy

Full Screen

1class ExampleSpec: FunSpec() {2 init {3 }4}5class ExampleSpec: BehaviorSpec() {6 init {7 }8}9class ExampleSpec: DescribeSpec() {10 init {11 }12}13class ExampleSpec: FeatureSpec() {14 init {15 }16}17class ExampleSpec: FreeSpec() {18 init {19 }20}21class ExampleSpec: ShouldSpec() {22 init {23 }24}25class ExampleSpec: StringSpec() {26 init {27 }28}29class ExampleSpec: WordSpec() {30 init {31 }32}33class ExampleSpec: ExpectSpec() {34 init {35 }36}

Full Screen

Full Screen

Spec.functionOverrideCallbacks

Using AI Code Generation

copy

Full Screen

1Spec.functionOverrideCallbacks(2beforeTest = { testDescriptor ->3},4afterTest = { testDescriptor, result ->5}6}7}8class ExampleSpec: FunSpec() {9init {10test("test1") {11}12test("test2") {13}14}15}

Full Screen

Full Screen

Spec.functionOverrideCallbacks

Using AI Code Generation

copy

Full Screen

1class MySpec : FunSpec() {2override fun listeners() = listOf(3object : SpecExecutionListener {4override fun beforeTest(testCase: TestCase) {5println("before test ${testCase.description.name}")6}7override fun afterTest(testCase: TestCase, result: TestResult) {8println("after test ${testCase.description.name}")9}10override fun afterSpec(spec: Spec) {11println("after spec")12}13override fun beforeSpec(spec: Spec) {14println("before spec")15}16})17}18override fun beforeTest(testCase: TestCase) {19println("before test ${testCase.description.name}")20}21override fun afterTest(testCase: TestCase, result: TestResult) {22println("after test ${testCase.description.name}")23}24override fun afterSpec(spec: Spec) {25println("after spec")26}27override fun beforeSpec(spec: Spec) {28println("before spec")29}30fun test1() {31println("test1")32}33}34fun test2() {35println("test2")36}37}38class MySpec : FunSpec() {39override fun beforeContainer(testCase: TestCase) {40println("before container")41}42override fun afterContainer(testCase: TestCase, result: TestResult) {43println("after container")44}45override fun beforeTest(testCase: TestCase) {46println("before test ${testCase.description.name}")47}48override fun afterTest(testCase: TestCase, result: TestResult) {49println("after test ${testCase.description.name}")50}51override fun afterSpec(spec: Spec) {52println("after spec")53}54override fun beforeSpec(spec: Spec) {55println("before spec")56}57inner class NestedSpec : FunSpec({58beforeContainer {59println("before nested container")60}61afterContainer {62println("after nested container")63}64test("test1")

Full Screen

Full Screen

Spec.functionOverrideCallbacks

Using AI Code Generation

copy

Full Screen

1class MySpec : FunSpec() {2init {3functionOverrideCallbacks(4{ testContext ->5testContext.registerTestCase(6TestCaseConfig(7{ println ( "my test" ) }8}9}10}11class MySpec : FunSpec() {12init {13scopes(14{ testContext ->15testContext.registerTestCase(16TestCaseConfig(17{ println ( "my test" ) }18}19}20class MySpec : FunSpec() {21init {22afterSpec {23println ( "after spec" )24}25}26}27class MySpec : FunSpec() {28init {29afterTest {30println ( "after test" )31}32}33}34class MySpec : FunSpec() {35init {

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 method in listeners

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful