How to use MountableExtension class of io.kotest.core.extensions package

Best Kotest code snippet using io.kotest.core.extensions.MountableExtension

JdbcTestContainerExtension.kt

Source:JdbcTestContainerExtension.kt Github

copy

Full Screen

1package io.kotest.extensions.testcontainers2import com.zaxxer.hikari.HikariDataSource3import io.kotest.core.extensions.MountableExtension4import io.kotest.core.listeners.AfterSpecListener5import io.kotest.core.listeners.TestListener6import io.kotest.core.spec.Spec7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestResult9import io.kotest.core.test.TestType10import io.kotest.core.test.isRootTest11import kotlinx.coroutines.Dispatchers12import kotlinx.coroutines.withContext13import org.testcontainers.containers.JdbcDatabaseContainer14import java.io.PrintWriter15import java.sql.Connection16import java.util.logging.Logger17import javax.sql.DataSource18/**19 * A Kotest [MountableExtension] for [JdbcDatabaseContainer]s that will launch the container20 * upon install, and close after the spec has completed.21 *22 * This extension will create a pooled [HikariDataSource] attached to the database and23 * return that to the user as the materialized value.24 *25 * The pool can be configured in the mount configure method.26 *27 * Note: This extension requires Kotest 5.0+28 *29 * @param container the specific test container type30 * @param lifecycleMode determines how the container should be reset between tests31 *32 * @since 1.1.033 */34class JdbcTestContainerExtension(35 private val container: JdbcDatabaseContainer<Nothing>,36 private val lifecycleMode: LifecycleMode = LifecycleMode.Spec,37) : MountableExtension<TestContainerHikariConfig, DataSource>, AfterSpecListener, TestListener {38 private val ds = SettableDataSource(null)39 private var configure: TestContainerHikariConfig.() -> Unit = {}40 override fun mount(configure: TestContainerHikariConfig.() -> Unit): DataSource {41 this.configure = configure42 if (lifecycleMode == LifecycleMode.Spec) {43 container.start()44 ds.setDataSource(createDataSource())45 }46 return ds47 }48 private fun createDataSource(): HikariDataSource {49 val config = TestContainerHikariConfig()50 config.jdbcUrl = container.jdbcUrl51 config.username = container.username...

Full Screen

Full Screen

SharedJdbcDatabaseContainerExtension.kt

Source:SharedJdbcDatabaseContainerExtension.kt Github

copy

Full Screen

1package io.kotest.extensions.testcontainers2import com.zaxxer.hikari.HikariConfig3import com.zaxxer.hikari.HikariDataSource4import io.kotest.core.extensions.MountableExtension5import io.kotest.core.listeners.AfterProjectListener6import io.kotest.core.listeners.AfterSpecListener7import io.kotest.core.listeners.AfterTestListener8import io.kotest.core.listeners.BeforeSpecListener9import io.kotest.core.listeners.BeforeTestListener10import io.kotest.core.spec.Spec11import io.kotest.core.test.TestCase12import io.kotest.core.test.TestResult13import org.testcontainers.containers.JdbcDatabaseContainer14import java.sql.Connection15/**16 * A Kotest [MountableExtension] for [JdbcDatabaseContainer]s that are started the first time they are17 * installed in a test, and then shared throughout the same gradle module. The container is shutdown18 * after all specs have completed.19 *20 * If no spec is executed that installs a particular container, then that container is never started.21 *22 * This extension will create a pooled [HikariDataSource] attached to the database and23 * return that to the user as the materialized value.24 *25 * The Hikari pool can be configured in the constructor through the [configure] parameter, or through26 * the install method per spec. If the latter option is used, then only the configure function from27 * the install where the container is first started will be executed.28 *29 * Note: This extension requires Kotest 5.0+30 *31 * @param container the specific database test container type32 * @param beforeSpec a beforeSpec callback33 * @param afterSpec an afterSpec callback34 * @param beforeTest a beforeTest callback35 * @param afterTest a afterTest callback36 * @param afterStart called one time, after the container is started37 * @param configure a callback to configure the [HikariConfig] instance that is used to create the [HikariDataSource].38 *39 * @since 1.3.040 */41class SharedJdbcDatabaseContainerExtension(42 private val container: JdbcDatabaseContainer<*>,43 private val beforeTest: suspend (HikariDataSource) -> Unit = {},44 private val afterTest: suspend (HikariDataSource) -> Unit = {},45 private val beforeSpec: suspend (HikariDataSource) -> Unit = {},46 private val afterSpec: suspend (HikariDataSource) -> Unit = {},47 private val afterStart: (HikariDataSource) -> Unit = {},48 private val configure: TestContainerHikariConfig.() -> Unit = {},49) : MountableExtension<TestContainerHikariConfig, HikariDataSource>,50 AfterProjectListener,51 BeforeTestListener,52 BeforeSpecListener,53 AfterTestListener,54 AfterSpecListener {55 private var ds: HikariDataSource? = null56 override fun mount(configure: TestContainerHikariConfig.() -> Unit): HikariDataSource {57 if (!container.isRunning) {58 container.start()59 ds = createDataSource().apply(afterStart)60 }61 return ds ?: error("DataSource was not initialized")62 }63 override suspend fun afterProject() {...

Full Screen

Full Screen

SharedTestContainerExtension.kt

Source:SharedTestContainerExtension.kt Github

copy

Full Screen

1package io.kotest.extensions.testcontainers2import io.kotest.core.extensions.MountableExtension3import io.kotest.core.listeners.AfterProjectListener4import io.kotest.core.listeners.AfterSpecListener5import io.kotest.core.listeners.AfterTestListener6import io.kotest.core.listeners.BeforeSpecListener7import io.kotest.core.listeners.BeforeTestListener8import io.kotest.core.spec.Spec9import io.kotest.core.test.TestCase10import io.kotest.core.test.TestResult11import org.testcontainers.containers.GenericContainer12/**13 * A Kotest [MountableExtension] for [GenericContainer]s that are started the first time they are14 * installed in a test, and then shared throughout the same gradle module. The container is shutdown15 * after all specs have completed.16 *17 * If no spec is executed that installs a particular container, then that container is never started.18 *19 * The returned materialized value can be adapted through the [mapper] parameter, to allow returning something other20 * than the raw container. For example, you could return a RedisClient that was preconnected to a redis container,21 * rather than returning the container itself.22 *23 * Note: This extension requires Kotest 5.0+24 *25 * @param container the specific database test container type26 * @param beforeSpec a beforeSpec callback, can be used to configure the container.27 * @param afterSpec an afterSpec callback, can be used to configure the container.28 * @param beforeTest a beforeTest callback, can be used to configure the container.29 * @param afterTest a afterTest callback, can be used to configure the container.30 * @param configure called one time after the container is started. Can configure the container without needing to31 * specify the configuration code at every use site.32 * @param mapper optional mapping function to adapt the materialized value.33 *34 * @since 1.3.035 */36class SharedTestContainerExtension<T : GenericContainer<*>, U>(37 private val container: T,38 private val beforeTest: suspend (T) -> Unit = {},39 private val afterTest: suspend (T) -> Unit = {},40 private val beforeSpec: suspend (T) -> Unit = {},41 private val afterSpec: suspend (T) -> Unit = {},42 private val configure: T.() -> Unit = {},43 private val mapper: T.() -> U,44) : MountableExtension<T, U>,45 AfterProjectListener,46 BeforeTestListener,47 BeforeSpecListener,48 AfterTestListener,49 AfterSpecListener {50 companion object {51 operator fun <T : GenericContainer<*>> invoke(52 container: T,53 beforeTest: (T) -> Unit = {},54 afterTest: (T) -> Unit = {},55 beforeSpec: (T) -> Unit = {},56 afterSpec: (T) -> Unit = {},57 configure: T.() -> Unit = {},58 ): SharedTestContainerExtension<T, T> {...

Full Screen

Full Screen

TestContainerExtension.kt

Source:TestContainerExtension.kt Github

copy

Full Screen

1package io.kotest.extensions.testcontainers2import io.kotest.core.extensions.MountableExtension3import io.kotest.core.listeners.AfterSpecListener4import io.kotest.core.listeners.TestListener5import io.kotest.core.spec.Spec6import io.kotest.core.test.TestCase7import io.kotest.core.test.TestResult8import io.kotest.core.test.TestType9import io.kotest.core.test.isRootTest10import kotlinx.coroutines.Dispatchers11import kotlinx.coroutines.withContext12import org.testcontainers.containers.GenericContainer13import org.testcontainers.lifecycle.TestLifecycleAware14import java.util.Optional15class TestContainerExtension<T : GenericContainer<out T>>(16 private val container: GenericContainer<out T>,17 private val lifecycleMode: LifecycleMode = LifecycleMode.Spec,18) : MountableExtension<T, T>, TestListener, AfterSpecListener {19 companion object {20 operator fun invoke(name: String): TestContainerExtension<GenericContainer<Nothing>> =21 TestContainerExtension(GenericContainer<Nothing>(name))22 operator fun invoke(name: String, lifecycleMode: LifecycleMode): TestContainerExtension<GenericContainer<Nothing>> =23 TestContainerExtension(GenericContainer<Nothing>(name), lifecycleMode)24 }25 override fun mount(configure: T.() -> Unit): T {26 (container as T).configure()27 if (lifecycleMode == LifecycleMode.Spec) {28 container.start()29 }30 return container31 }32 override suspend fun afterSpec(spec: Spec) {...

Full Screen

Full Screen

MountableExtensionTest.kt

Source:MountableExtensionTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.extensions2import io.kotest.core.extensions.MountableExtension3import io.kotest.core.extensions.install4import io.kotest.core.listeners.BeforeSpecListener5import io.kotest.core.spec.Spec6import io.kotest.core.spec.style.FunSpec7import io.kotest.matchers.shouldBe8import java.util.concurrent.atomic.AtomicBoolean9class MountableExtensionTest : FunSpec() {10 private val mountable = MyMountable()11 private val control = install(mountable) {12 a = "bar"13 }14 init {15 test("mountable extensions should invoke configuration block") {16 control.a shouldBe "bar"17 }18 test("mountable extensions should be installed as regular extensions") {19 mountable.before.get() shouldBe true20 }21 }22}23data class Config(var a: String)24class MyMountable : MountableExtension<Config, Config>, BeforeSpecListener {25 val before = AtomicBoolean(false)26 override suspend fun beforeSpec(spec: Spec) {27 before.set(true)28 }29 override fun mount(configure: (Config) -> Unit): Config {30 val config = Config("foo")31 configure(config)32 return config33 }34}...

Full Screen

Full Screen

MountableExtension.kt

Source:MountableExtension.kt Github

copy

Full Screen

1package io.kotest.core.extensions2import io.kotest.core.spec.Spec3/**4 * A [MountableExtension] is an [Extension] that can return a materialized value to the5 * user and allows for a configuration block.6 *7 * This allows extensions to return control objects which differ from the extension itself.8 *9 * For example:10 *11 * class MyTest : FunSpec() {12 * init {13 * val kafka = install(EmbeddedKafka) {14 * port = 909215 * }16 * }17 * }18 *19 * Here `kafka` is a materialized value that contains details of the host/port of the20 * started kafka instance and `EmbeddedKafka` is the extension itself.21 *22 */23interface MountableExtension<CONFIG, MATERIALIZED> : Extension {24 // cannot be suspending as it is invoked by install that is used in constructors25 fun mount(configure: CONFIG.() -> Unit): MATERIALIZED26}27// cannot be suspending as it is used in constructors28fun <CONFIG, MATERIALIZED> Spec.install(29 mountable: MountableExtension<CONFIG, MATERIALIZED>,30 configure: CONFIG.() -> Unit = {}31): MATERIALIZED {32 extensions(mountable)33 return mountable.mount(configure)34}...

Full Screen

Full Screen

MountableExtension

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.extensions.MountableExtension2class MyExtension : MountableExtension {3override fun beforeTest(testCase: TestCase) {4println("Before test: ${testCase.description.fullName()}")5}6override fun afterTest(testCase: TestCase, result: TestResult) {7println("After test: ${testCase.description.fullName()}")8}9}10import io.kotest.core.extensions.MountableExtension11class MyExtension : MountableExtension {12override fun beforeContainer(container: TestContainer) {13println("Before container: ${container.description.fullName()}")14}15override fun afterContainer(container: TestContainer, result: TestResult) {16println("After container: ${container.description.fullName()}")17}18}19import io.kotest.core.extensions.MountableExtension20class MyExtension : MountableExtension {21override fun beforeSpec(spec: Spec) {22println("Before spec: ${spec::class.qualifiedName}")23}24override fun afterSpec(spec: Spec, results: Map<TestCase, TestResult>) {25println("After spec: ${spec::class.qualifiedName}")26}27}28import io.kotest.core.extensions.MountableExtension29class MyExtension : MountableExtension {30override fun beforeProject() {31println("Before project")32}33override fun afterProject() {34println("After project")35}36}37import io.kotest.core.extensions.MountableExtension38class MyExtension : MountableExtension {39override fun afterSpecClass(spec: KClass<out Spec>, results: Map<TestCase, TestResult>) {40println("After spec class: ${spec.qualifiedName}")41}42override fun beforeSpecClass(spec: KClass<out Spec>) {43println("Before spec class: ${spec.qualifiedName}")44}45}46import io.kotest.core.extensions.MountableExtension47class MyExtension : MountableExtension {48override fun afterTestSuite(suite: TestSuite, results: Map<TestCase, TestResult>) {49println("After test suite: ${suite.description.fullName()}")50}51override fun beforeTestSuite(suite: TestSuite) {52println("Before test

Full Screen

Full Screen

MountableExtension

Using AI Code Generation

copy

Full Screen

1val mountableExtension = object : MountableExtension { 2override fun mount() { 3}4override fun unmount() { 5} 6}7extensions(mountableExtension) 8}9I am not able to find a way to unmount the extension after the test execution. I have tried using the unmount() method of MountableExtension class but that is not getting called.

Full Screen

Full Screen

MountableExtension

Using AI Code Generation

copy

Full Screen

1val mountableExtension = object : MountableExtension {2override fun mount(project: Project) {3}4}5registerExtension(mountableExtension)6}7}8The mount() and unmount() methods are

Full Screen

Full Screen

MountableExtension

Using AI Code Generation

copy

Full Screen

1val mountableExtension = MountableExtension (2MyExtension()3registerExtension(mountableExtension)4val mountableExtension = MountableExtension (5MyExtension()6registerExtension(mountableExtension)7val mountableExtension = MountableExtension (8MyExtension()9registerExtension(mountableExtension)10val mountableExtension = MountableExtension (11MyExtension()12registerExtension(mountableExtension)13val mountableExtension = MountableExtension (14MyExtension()15registerExtension(mountableExtension)16val mountableExtension = MountableExtension (17MyExtension()18registerExtension(mountableExtension)19val mountableExtension = MountableExtension (20MyExtension()21registerExtension(mountableExtension)22val mountableExtension = MountableExtension (23MyExtension()24registerExtension(mountableExtension)25val mountableExtension = MountableExtension (

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 MountableExtension

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful