Best Kotest code snippet using io.kotest.engine.test.scopes.TerminalTestScope
createSpecExecutorDelegate.kt
Source:createSpecExecutorDelegate.kt
...10import io.kotest.engine.describe11import io.kotest.engine.it12import io.kotest.engine.listener.TestEngineListener13import io.kotest.engine.test.TestCaseExecutor14import io.kotest.engine.test.scopes.TerminalTestScope15import io.kotest.engine.test.interceptors.testNameEscape16import io.kotest.engine.test.names.getDisplayNameFormatter17import io.kotest.engine.test.status.isEnabledInternal18import io.kotest.engine.xit19import io.kotest.mpp.bestName20import kotlinx.coroutines.GlobalScope21import kotlinx.coroutines.promise22import kotlin.coroutines.coroutineContext23@ExperimentalKotest24internal actual fun createSpecExecutorDelegate(25 listener: TestEngineListener,26 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,27 configuration: ProjectConfiguration,28): SpecExecutorDelegate = JavascriptSpecExecutorDelegate(configuration)29/**30 * Note: we need to use this: https://youtrack.jetbrains.com/issue/KT-2222831 */32@ExperimentalKotest33internal class JavascriptSpecExecutorDelegate(private val configuration: ProjectConfiguration) : SpecExecutorDelegate {34 private val formatter = getDisplayNameFormatter(35 configuration.registry,36 configuration37 )38 private val materializer = Materializer(configuration)39 override suspend fun execute(spec: Spec): Map<TestCase, TestResult> {40 val cc = coroutineContext41 // we use the spec itself as an outer/parent test.42 describe(testNameEscape(spec::class.bestName())) {43 materializer.materialize(spec).forEach { root ->44 val testDisplayName = testNameEscape(formatter.format(root))45 // todo find a way to delegate this to the test case executor46 val enabled = root.isEnabledInternal(configuration)47 if (enabled.isEnabled) {48 // we have to always invoke `it` to start the test so that the js test framework doesn't exit49 // before we invoke our callback. This also gives us the handle to the done callback.50 val test = it(testDisplayName) { done ->51 // ideally we'd just launch the executor and have the listener setup the test52 // but we can't launch a promise inside the describe and have it resolve the "it"53 // this means we must duplicate the isEnabled check outside of the executor54 GlobalScope.promise {55 TestCaseExecutor(56 PromiseTestCaseExecutionListener(done),57 NoopCoroutineDispatcherFactory,58 configuration59 ).execute(root, TerminalTestScope(root, cc))60 }61 // we don't want to return the promise as the js frameworks will use that for test resolution62 // instead of the done callback, and we prefer the callback as it allows for custom timeouts63 Unit64 }65 // some frameworks default to a 2000 timeout,66 // here we set to a high number and use the timeout support kotest provides via coroutines67 test.timeout(Int.MAX_VALUE)68 Unit69 } else {70 xit(testDisplayName) {}71 }72 }73 }...
TestFinishedExecutionInterceptorTest.kt
Source:TestFinishedExecutionInterceptorTest.kt
...7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestResult9import io.kotest.core.test.TestType10import io.kotest.engine.test.AbstractTestCaseExecutionListener11import io.kotest.engine.test.scopes.TerminalTestScope12import io.kotest.engine.test.interceptors.TestFinishedInterceptor13import io.kotest.matchers.shouldBe14class TestFinishedExecutionInterceptorTest : FunSpec({15 test("should notify of test finishes") {16 val tc = TestCase(17 TestFinishedExecutionInterceptorTest::class.toDescriptor().append("foo"),18 TestName("foo"),19 TestFinishedExecutionInterceptorTest(),20 {},21 sourceRef(),22 TestType.Test23 )24 val context = TerminalTestScope(tc, coroutineContext)25 var finished = false26 val listener = object : AbstractTestCaseExecutionListener() {27 override suspend fun testFinished(testCase: TestCase, result: TestResult) {28 super.testFinished(testCase, result)29 finished = true30 }31 }32 TestFinishedInterceptor(listener).intercept(tc, context) { _, _ -> TestResult.success(0) }33 finished shouldBe true34 }35 test("should notify of test ignores") {36 val tc = TestCase(37 TestFinishedExecutionInterceptorTest::class.toDescriptor().append("!foo"),38 TestName("!foo"),39 TestFinishedExecutionInterceptorTest(),40 {},41 sourceRef(),42 TestType.Test43 )44 val context = TerminalTestScope(tc, coroutineContext)45 var ignored = false46 var r: String? = null47 val listener = object : AbstractTestCaseExecutionListener() {48 override suspend fun testIgnored(testCase: TestCase, reason: String?) {49 ignored = true50 r = reason51 }52 }53 TestFinishedInterceptor(listener).intercept(tc, context) { _, _ -> TestResult.Ignored("wobble") }54 ignored shouldBe true55 r shouldBe "wobble"56 }57})...
EnabledCheckTestExecutionInterceptorTest.kt
Source:EnabledCheckTestExecutionInterceptorTest.kt
...7import io.kotest.core.spec.style.FunSpec8import io.kotest.core.test.TestCase9import io.kotest.core.test.TestResult10import io.kotest.core.test.TestType11import io.kotest.engine.test.scopes.TerminalTestScope12import io.kotest.engine.test.interceptors.EnabledCheckInterceptor13import io.kotest.matchers.shouldBe14import kotlin.time.Duration.Companion.seconds15class EnabledCheckTestExecutionInterceptorTest : FunSpec({16 test("should invoke chain function if test is enabled") {17 val tc = TestCase(18 EnabledCheckTestExecutionInterceptorTest::class.toDescriptor().append("foo"),19 TestName("foo"),20 EnabledCheckTestExecutionInterceptorTest(),21 {},22 sourceRef(),23 TestType.Test24 )25 val context = TerminalTestScope(tc, coroutineContext)26 // the test starts with ! so should not be enabled, therefore the chain should be ignored27 var fired = false28 EnabledCheckInterceptor(ProjectConfiguration()).intercept(tc, context) { _, _ ->29 fired = true30 TestResult.Success(0.seconds)31 }32 fired shouldBe true33 }34 test("should skip chain function if test is not enabled") {35 val tc = TestCase(36 EnabledCheckTestExecutionInterceptorTest::class.toDescriptor().append("!foo"),37 TestName("!foo"),38 EnabledCheckTestExecutionInterceptorTest(),39 {},40 sourceRef(),41 TestType.Test42 )43 val context = TerminalTestScope(tc, coroutineContext)44 // the test starts with ! so should not be enabled, therefore the chain should be ignored45 EnabledCheckInterceptor(ProjectConfiguration()).intercept(tc, context) { _, _ -> error("boom") }46 }47})...
ExceptionCapturingTestExecutionInterceptor.kt
Source:ExceptionCapturingTestExecutionInterceptor.kt
...6import io.kotest.core.spec.style.FunSpec7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestType9import io.kotest.engine.test.interceptors.ExceptionCapturingInterceptor10import io.kotest.engine.test.scopes.TerminalTestScope11import io.kotest.matchers.booleans.shouldBeTrue12import kotlin.time.TimeSource13class ExceptionCapturingTestExecutionInterceptorTest : FunSpec({14 test("ExceptionCapturingTestExecutionInterceptor should capture assertion errors") {15 val tc = TestCase(16 ExceptionCapturingTestExecutionInterceptorTest::class.toDescriptor().append("foo"),17 TestName("foo"),18 ExceptionCapturingTestExecutionInterceptorTest(),19 {},20 sourceRef(),21 TestType.Test22 )23 val context = TerminalTestScope(tc, coroutineContext)24 ExceptionCapturingInterceptor(TimeSource.Monotonic.markNow())25 .intercept(tc, context) { _, _ -> throw AssertionError("boom") }26 .isFailure.shouldBeTrue()27 }28 test("ExceptionCapturingTestExecutionInterceptor should capture exceptions") {29 val tc = TestCase(30 ExceptionCapturingTestExecutionInterceptorTest::class.toDescriptor().append("foo"),31 TestName("foo"),32 ExceptionCapturingTestExecutionInterceptorTest(),33 {},34 sourceRef(),35 TestType.Test36 )37 val context = TerminalTestScope(tc, coroutineContext)38 ExceptionCapturingInterceptor(TimeSource.Monotonic.markNow())39 .intercept(tc, context) { _, _ -> error("boom") }40 .isError.shouldBeTrue()41 }42})...
TerminalTestScope.kt
Source:TerminalTestScope.kt
...5import kotlin.coroutines.CoroutineContext6/**7 * A [TestScope] that errors on registration attempts of nested tests.8 */9class TerminalTestScope(10 override val testCase: TestCase,11 override val coroutineContext: CoroutineContext12) : TestScope {13 override suspend fun registerTestCase(nested: NestedTest) {14 error("Nested tests are not supported")15 }16}...
TerminalTestScope
Using AI Code Generation
1 import io.kotest.core.spec.style.FunSpec2 import io.kotest.engine.test.scopes.TerminalTestScope3 import io.kotest.matchers.shouldBe4 class MySpec : FunSpec({5 test("hello") {6 }7 })
TerminalTestScope
Using AI Code Generation
1 import io.kotest.engine.test.scopes.TerminalTestScope2 import io.kotest.core.spec.style.FunSpec3 import io.kotest.matchers.shouldBe4 class MyTest : FunSpec({5 test("test 1") {6 }7 })
TerminalTestScope
Using AI Code Generation
1import io.kotest.core.spec.style.FunSpec2class MyTest : FunSpec({3 test("some test").config(enabled = false) {4 }5})6import io.kotest.core.spec.style.FunSpec7class MyTest : FunSpec({8 test("some test").config(enabled = false) {9 }10})11import io.kotest.core.spec.style.FunSpec12class MyTest : FunSpec({13 test("some test").config(enabled = false) {14 }15})16import io.kotest.core.spec.style.FunSpec17class MyTest : FunSpec({18 test("some test").config(enabled = false) {19 }20})21import io.kotest.core.spec.style.FunSpec22class MyTest : FunSpec({23 test("some test").config(enabled = false) {24 }25})26import io.kotest.core.spec.style.FunSpec27class MyTest : FunSpec({28 test("some test").config(enabled = false) {29 }30})31import io.kotest.core.spec.style.FunSpec32class MyTest : FunSpec({33 test("some test").config(enabled = false) {34 }35})36import io.kotest.core.spec.style.FunSpec37class MyTest : FunSpec({38 test("some test").config(enabled = false) {39 }40})41import io.kotest.core.spec.style.FunSpec42class MyTest : FunSpec({43 test("some test").config(enabled = false) {
TerminalTestScope
Using AI Code Generation
1import io.kotest.core.config.configuration 2import io.kotest.core.config.extensions 3import io.kotest.core.extensions.Extension 4import io.kotest.core.extensions.TestCaseExtension 5import io.kotest.core.extensions.TestExtension 6import io.kotest.core.extensions.TestListener 7import io.kotest.core.extensions.TestListenerExtension 8import io.kotest.core.listeners.TestListener 9import io.kotest.core.spec.Spec 10import io.kotest.core.test.TestCase 11import io.kotest.core.test.TestResult 12import io.kotest.core.test.TestType 13import io.kotest.core.test.createTestName 14import io.kotest.engine.test.contexts.TerminalTestContext 15import io.kotest.engine.test.interceptors.TestCaseExecutionInterceptors 16import io.kotest.engine.test.interceptors.TestExecutionInterceptors 17import io.kotest.engine.test.interceptors.TestTimeoutInterceptor 18import io.kotest.engine.test.interceptors.createTestTimeoutException 19import io.kotest.engine.test.status.TestStatus 20import io.kotest.mpp.bestName 21import io.kotest.mpp.log 22import kotlin.time.Duration 23import kotlin.time.ExperimentalTime24@OptIn(ExperimentalTime::class) 25class TerminalTestScope( 26val test: suspend () -> TestResult, 27) : TestScope {28get() = testCase.name29get() = testCase.description30get() = testCase.spec31get() = testCase.type32get() = testCase.source33get() = testCase.tags34get() = configuration.extensions()35get() = testCase.config36override fun registerTestCaseExtension(extension: TestCaseExtension) {
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!!