Best Kotest code snippet using io.kotest.core.spec.AutoCloseable.close
TestConfiguration.kt
Source:TestConfiguration.kt
...111 _tags = _tags + tags.toSet()112 }113 fun appliedTags() = _tags114 /**115 * Registers an [AutoCloseable] to be closed when the spec is completed.116 */117 @Suppress("PropertyName")118 fun <T : AutoCloseable> autoClose(closeable: T): T =119 autoClose(lazy(LazyThreadSafetyMode.NONE) { closeable }).value120 /**121 * Registers a lazy [AutoCloseable] to be closed when the spec is completed.122 */123 fun <T : AutoCloseable> autoClose(closeable: Lazy<T>): Lazy<T> {124 _autoCloseables = listOf(closeable) + _autoCloseables125 return closeable126 }127 /**128 * Registers a callback to be executed before every [TestCase].129 *130 * The [TestCase] about to be executed is provided as the parameter.131 */132 open fun beforeTest(f: BeforeTest) {133 register(object : TestListener {134 override suspend fun beforeAny(testCase: TestCase) {135 f(testCase)136 }137 })138 }139 /**...
LoungeControllerTest.kt
Source:LoungeControllerTest.kt
...135 notified shouldBe 0136 }137 test("Possess tag during build") {138 val tags = mutableListOf<AutoCloseable>()139 val closedKey = mutableListOf<String>()140 var key = "key1"141 val controller = object : LoungeController(owner.lifecycle, dispatcher) {142 override suspend fun buildModels() {143 val k = key144 val tag = possessTagDuringBuilding(k, AutoCloseable::class) {145 AutoCloseable { closedKey += k }146 }147 tags += tag148 }149 }150 owner.lifecycle.currentState = Lifecycle.State.STARTED151 controller.requestModelBuild()152 controller.requestModelBuild()153 tags.size shouldBe 2154 tags.distinct().size shouldBe 1155 closedKey.isEmpty() shouldBe true156 key = "key2"157 controller.requestModelBuild()158 tags.size shouldBe 3159 tags.distinct().size shouldBe 2160 closedKey shouldBe listOf("key1")161 controller.close()162 closedKey shouldBe listOf("key1", "key2")163 }164 test("Possess tag during build can only be invoked once for the same key") {165 var testComplete = false166 val controller = object : LoungeController(owner.lifecycle, dispatcher) {167 override suspend fun buildModels() {168 possessTagDuringBuilding("key", Any::class) { Any() }169 shouldThrowAny { possessTagDuringBuilding("key", Any::class) { Any() } }170 testComplete = true171 }172 }173 owner.lifecycle.currentState = Lifecycle.State.STARTED174 controller.requestModelBuild()175 testComplete shouldBe true176 }...
DefaultJobExecutorsSpec.kt
Source:DefaultJobExecutorsSpec.kt
...13 blockingMaxJobs = 114 nonBlockingMaxJobs = 115 }16 private fun newTestee(): JobExecutors = autoClose(object : AutoCloseable, JobExecutors by DefaultJobExecutors(config) {17 override fun close() {18 shutdown()19 }20 })21 init {22 should("execute coroutine when resources are available again") {23 forAll(*newTestee().dispatchers.map { row(it.value) }.toTypedArray()) { dispatcher ->24 dispatcher.shouldNotBeNull()25 val latch1 = CountDownLatch(1)26 val latch2 = CountDownLatch(2)27 dispatcher.canExecute() shouldBe true28 CoroutineScope(dispatcher.coroutineDispatcher).launch {29 latch1.await()30 }31 dispatcher.canExecute() shouldBe false...
AutoCloseTest.kt
Source:AutoCloseTest.kt
1package com.sksamuel.kotest.engine.autoclose2import io.kotest.core.spec.Spec3import io.kotest.core.spec.style.StringSpec4import io.kotest.matchers.shouldBe5import java.util.concurrent.atomic.AtomicInteger6class AutoCloseTest : StringSpec() {7 private val first = autoClose(FirstAutoclose)8 private val second = autoClose(SecondAutoclose)9 private val third = autoClose(ThirdAutoclose)10 init {11 "should close resources in reverse order" {12 // Test will be verified in AfterSpec13 }14 }15 override suspend fun afterSpec(spec: Spec) {16 FirstAutoclose.closed shouldBe 317 SecondAutoclose.closed shouldBe 218 ThirdAutoclose.closed shouldBe 119 }20}21private val closedNumber = AtomicInteger(0)22private object FirstAutoclose : AutoCloseable {23 var closed = -124 override fun close() {25 closed = closedNumber.incrementAndGet()26 }27}28private object SecondAutoclose : AutoCloseable {29 var closed = -130 override fun close() {31 closed = closedNumber.incrementAndGet()32 }33}34private object ThirdAutoclose : AutoCloseable {35 var closed = -136 override fun close() {37 closed = closedNumber.incrementAndGet()38 }39}...
AutoCloseable.kt
Source:AutoCloseable.kt
...11 limitations under the License.12*/13package batect.dockerclient14import io.kotest.core.TestConfiguration15actual fun <T : AutoCloseable> TestConfiguration.closeAfterTest(instance: T): T {16 autoClose(object : io.kotest.core.spec.AutoCloseable {17 override fun close() {18 instance.close()19 }20 })21 return instance22}...
MockServerListener.kt
Source:MockServerListener.kt
...18 }19 override suspend fun afterSpec(spec: Spec) {20 mockServer.stop()21 }22 override fun close() {23 mockServer.stop()24 }25}...
AutoCloseDslTest.kt
Source:AutoCloseDslTest.kt
1package com.sksamuel.kotest.engine.autoclose2import io.kotest.core.spec.style.FunSpec3import io.kotest.matchers.booleans.shouldBeFalse4import io.kotest.matchers.booleans.shouldBeTrue5class AutoCloseDslTest : FunSpec({6 var beforeTests = true7 var closed = false8 val closeme = AutoCloseable { closed = true }9 autoClose(closeme)10 val lazyClosed by autoClose(lazy {11 beforeTests = false12 LazyCloseable()13 })14 beforeTests.shouldBeTrue()15 test("auto close with dsl method") {16 closed.shouldBeFalse()17 }18 test("lazy auto close with dsl method") {19 lazyClosed.closed.shouldBeFalse()20 }21 afterSpec {22 closed.shouldBeTrue()23 lazyClosed.closed.shouldBeTrue()24 }25}) {26 class LazyCloseable : AutoCloseable {27 var closed = false28 override fun close() {29 closed = true30 }31 }32}...
LazyAutoCloseTest.kt
Source:LazyAutoCloseTest.kt
1package com.sksamuel.kotest.engine.autoclose2import io.kotest.core.spec.style.StringSpec3import io.kotest.matchers.booleans.shouldBeFalse4import io.kotest.matchers.booleans.shouldBeTrue5class LazyAutoCloseTest : StringSpec({6 var fullLazy = true7 var closed = false8 val closeme = AutoCloseable { closed = true }9 autoClose(lazy {10 fullLazy = false11 closeme12 })13 "autoClose lazy should be lazy" {14 fullLazy.shouldBeTrue()15 closed.shouldBeFalse()16 }17 afterProject {18 // If never used in the Spec it should never get initialised, or closed19 fullLazy.shouldBeTrue()20 closed.shouldBeFalse()21 }22})...
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!!