How to use TestCaseExecutionListenerToTestEngineListenerAdapter class of io.kotest.engine.test.listener package

Best Kotest code snippet using io.kotest.engine.test.listener.TestCaseExecutionListenerToTestEngineListenerAdapter

SingleInstanceSpecRunner.kt

Source:SingleInstanceSpecRunner.kt Github

copy

Full Screen

...12import io.kotest.engine.spec.Materializer13import io.kotest.engine.spec.SpecExtensions14import io.kotest.engine.spec.SpecRunner15import io.kotest.engine.test.TestCaseExecutor16import io.kotest.engine.test.listener.TestCaseExecutionListenerToTestEngineListenerAdapter17import io.kotest.engine.test.scheduler.TestScheduler18import io.kotest.engine.test.scopes.DuplicateNameHandlingTestScope19import io.kotest.mpp.Logger20import io.kotest.mpp.bestName21import kotlinx.coroutines.coroutineScope22import java.util.concurrent.ConcurrentHashMap23import kotlin.coroutines.CoroutineContext24/**25 * Implementation of [SpecRunner] that executes all tests against the26 * same [Spec] instance. In other words, only a single instance of the spec class27 * is instantiated for all the test cases.28 */29@ExperimentalKotest30internal class SingleInstanceSpecRunner(31 listener: TestEngineListener,32 scheduler: TestScheduler,33 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,34 private val configuration: ProjectConfiguration,35) : SpecRunner(listener, scheduler, configuration) {36 private val results = ConcurrentHashMap<TestCase, TestResult>()37 private val extensions = SpecExtensions(configuration.registry)38 private val logger = Logger(SingleInstanceSpecRunner::class)39 override suspend fun execute(spec: Spec): Result<Map<TestCase, TestResult>> {40 logger.log { Pair(spec::class.bestName(), "executing spec $spec") }41 suspend fun interceptAndRun(context: CoroutineContext) = runCatching {42 val rootTests = materializer.materialize(spec)43 logger.log { Pair(spec::class.bestName(), "Materialized root tests: ${rootTests.size}") }44 launch(spec) {45 logger.log { Pair(it.name.testName, "Executing test $it") }46 runTest(it, context, null)47 }48 }49 try {50 return coroutineScope {51 extensions.beforeSpec(spec)52 .flatMap { interceptAndRun(coroutineContext) }53 .flatMap { SpecExtensions(configuration.registry).afterSpec(spec) }54 .map { results }55 }56 } catch (e: Exception) {57 e.printStackTrace()58 throw e59 }60 }61 /**62 * A [TestScope] that runs discovered tests as soon as they are registered in the same spec instance.63 *64 * This implementation tracks fail fast if configured via TestCase config or globally.65 */66 inner class SingleInstanceTestScope(67 override val testCase: TestCase,68 override val coroutineContext: CoroutineContext,69 private val parentScope: SingleInstanceTestScope?,70 ) : TestScope {71 // set to true if we failed fast and should ignore further tests72 private var skipRemaining = false73 // in the single instance runner we execute each nested test as soon as they are registered74 override suspend fun registerTestCase(nested: NestedTest) {75 logger.log { Pair(testCase.name.testName, "Nested test case discovered '${nested}") }76 val nestedTestCase = Materializer(configuration).materialize(nested, testCase)77 if (skipRemaining) {78 logger.log { Pair(testCase.name.testName, "Skipping test due to fail fast") }79 listener.testIgnored(nestedTestCase, "Skipping test due to fail fast")80 } else {81 // if running this nested test results in an error, we won't launch anymore nested tests82 val result = runTest(nestedTestCase, coroutineContext, this@SingleInstanceTestScope)83 if (result.isErrorOrFailure) {84 if (testCase.config.failfast || configuration.projectWideFailFast) {85 logger.log { Pair(testCase.name.testName, "Test failed - setting skipRemaining = true") }86 skipRemaining = true87 parentScope?.skipRemaining = true88 }89 }90 }91 }92 }93 private suspend fun runTest(94 testCase: TestCase,95 coroutineContext: CoroutineContext,96 parentScope: SingleInstanceTestScope?,97 ): TestResult {98 val testExecutor = TestCaseExecutor(99 TestCaseExecutionListenerToTestEngineListenerAdapter(listener),100 defaultCoroutineDispatcherFactory,101 configuration,102 )103 val scope = DuplicateNameHandlingTestScope(104 configuration.duplicateTestNameMode,105 SingleInstanceTestScope(testCase, coroutineContext, parentScope)106 )107 val result = testExecutor.execute(testCase, scope)108 results[testCase] = result109 return result110 }111}...

Full Screen

Full Screen

createSpecExecutorDelegate.kt

Source:createSpecExecutorDelegate.kt Github

copy

Full Screen

...8import io.kotest.engine.listener.TestEngineListener9import io.kotest.engine.test.TestCaseExecutor10import io.kotest.engine.test.scopes.DuplicateNameHandlingTestScope11import io.kotest.engine.test.scopes.InOrderTestScope12import io.kotest.engine.test.listener.TestCaseExecutionListenerToTestEngineListenerAdapter13import io.kotest.mpp.log14import kotlin.coroutines.coroutineContext15@ExperimentalKotest16internal actual fun createSpecExecutorDelegate(17 listener: TestEngineListener,18 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,19 configuration: ProjectConfiguration,20): SpecExecutorDelegate =21 DefaultSpecExecutorDelegate(listener, defaultCoroutineDispatcherFactory, configuration)22/**23 * A [SpecExecutorDelegate] that executes tests sequentially, using the calling thread24 * as the execution context for timeouts.25 */26@ExperimentalKotest27internal class DefaultSpecExecutorDelegate(28 private val listener: TestEngineListener,29 private val coroutineDispatcherFactory: CoroutineDispatcherFactory,30 private val configuration: ProjectConfiguration31) : SpecExecutorDelegate {32 private val materializer = Materializer(configuration)33 override suspend fun execute(spec: Spec): Map<TestCase, TestResult> {34 log { "DefaultSpecExecutorDelegate: Executing spec $spec" }35 materializer.materialize(spec)36 .forEach { testCase ->37 log { "DefaultSpecExecutorDelegate: Executing testCase $testCase" }38 val context = DuplicateNameHandlingTestScope(39 configuration.duplicateTestNameMode,40 InOrderTestScope(41 testCase,42 coroutineContext,43 configuration.duplicateTestNameMode,44 listener,45 coroutineDispatcherFactory,46 configuration47 )48 )49 TestCaseExecutor(50 TestCaseExecutionListenerToTestEngineListenerAdapter(listener),51 coroutineDispatcherFactory,52 configuration53 ).execute(testCase, context)54 }55 return emptyMap()56 }57}...

Full Screen

Full Screen

InOrderTestScope.kt

Source:InOrderTestScope.kt Github

copy

Full Screen

...9import io.kotest.core.test.TestScope10import io.kotest.engine.listener.TestEngineListener11import io.kotest.engine.spec.Materializer12import io.kotest.engine.test.TestCaseExecutor13import io.kotest.engine.test.listener.TestCaseExecutionListenerToTestEngineListenerAdapter14import io.kotest.mpp.log15import kotlin.coroutines.CoroutineContext16/**17 * A [TestScope] that executes nested tests as soon as they are discovered.18 */19@ExperimentalKotest20class InOrderTestScope(21 override val testCase: TestCase,22 override val coroutineContext: CoroutineContext,23 private val mode: DuplicateTestNameMode,24 private val listener: TestEngineListener,25 private val coroutineDispatcherFactory: CoroutineDispatcherFactory,26 private val configuration: ProjectConfiguration,27) : TestScope {28 private var failed = false29 override suspend fun registerTestCase(nested: NestedTest) {30 log { "InOrderTestScope: Nested test case discovered $nested" }31 val nestedTestCase = Materializer(configuration).materialize(nested, testCase)32 if (failed && (testCase.config.failfast || configuration.projectWideFailFast)) {33 log { "InOrderTestScope: A previous nested test failed and failfast is enabled - will mark this as ignored" }34 listener.testIgnored(nestedTestCase, "Failfast enabled on parent test")35 } else {36 val result = runTest(nestedTestCase, coroutineContext)37 if (result.isErrorOrFailure) {38 failed = true39 }40 }41 }42 private suspend fun runTest(43 testCase: TestCase,44 coroutineContext: CoroutineContext,45 ): TestResult {46 return TestCaseExecutor(47 TestCaseExecutionListenerToTestEngineListenerAdapter(listener),48 coroutineDispatcherFactory,49 configuration,50 ).execute(51 testCase,52 createSingleInstanceTestScope(53 testCase,54 coroutineContext,55 mode,56 listener,57 coroutineDispatcherFactory,58 configuration,59 )60 )61 }...

Full Screen

Full Screen

TestCaseExecutionListenerToTestEngineListenerAdapter.kt

Source:TestCaseExecutionListenerToTestEngineListenerAdapter.kt Github

copy

Full Screen

...5import io.kotest.engine.test.TestCaseExecutionListener6/**7 * Converts events fired to a [TestCaseExecutionListener] into events fired to a [TestEngineListener]8 */9class TestCaseExecutionListenerToTestEngineListenerAdapter(10 private val listener: TestEngineListener11) : TestCaseExecutionListener {12 override suspend fun testFinished(testCase: TestCase, result: TestResult) {13 listener.testFinished(testCase, result)14 }15 override suspend fun testIgnored(testCase: TestCase, reason: String?) {16 listener.testIgnored(testCase, reason)17 }18 override suspend fun testStarted(testCase: TestCase) {19 listener.testStarted(testCase)20 }21}...

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful