How to use SpecExecutor class of io.kotest.engine.spec package

Best Kotest code snippet using io.kotest.engine.spec.SpecExecutor

SpecExecutor.kt

Source:SpecExecutor.kt Github

copy

Full Screen

...43 * [io.kotest.engine.spec.interceptor.SpecInterceptor] are executed after the spec is created.44 *45 */46@ExperimentalKotest47class SpecExecutor(48 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,49 private val context: EngineContext,50) {51 private val logger = Logger(SpecExecutorDelegate::class)52 private val extensions = SpecExtensions(context.configuration.registry)53 private val listener = context.listener54 suspend fun execute(ref: SpecRef) {55 logger.log { Pair(ref.kclass.bestName(), "Received $ref") }56 referenceInterceptors(ref)57 }58 suspend fun execute(kclass: KClass<out Spec>) {59 execute(SpecRef.Reference(kclass))60 }61 private suspend fun referenceInterceptors(ref: SpecRef) {62 val interceptors = listOfNotNull(63 if (platform == Platform.JVM) EnabledIfSpecInterceptor(listener, context.configuration.registry) else null,64 IgnoredSpecInterceptor(listener, context.configuration.registry),65 SpecFilterInterceptor(listener, context.configuration.registry),66 SystemPropertySpecFilterInterceptor(listener, context.configuration.registry),67 TagsExcludedSpecInterceptor(listener, context.configuration),68 if (platform == Platform.JVM) RequiresTagSpecInterceptor(listener, context.configuration, context.configuration.registry) else null,69 SpecRefExtensionInterceptor(context.configuration.registry),70 SpecStartedInterceptor(listener),71 SpecFinishedInterceptor(listener),72 if (platform == Platform.JVM) ApplyExtensionsInterceptor(context.configuration.registry) else null,73 PrepareSpecInterceptor(context.configuration.registry),74 FinalizeSpecInterceptor(context.configuration.registry),75 )76 val innerExecute: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> = {77 createInstance(ref).flatMap { specInterceptors(it) }78 }79 logger.log { Pair(ref.kclass.bestName(), "Executing ${interceptors.size} reference interceptors") }80 interceptors.foldRight(innerExecute) { ext: SpecRefInterceptor, fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> ->81 { ref -> ext.intercept(ref, fn) }82 }.invoke(ref)83 }84 private suspend fun specInterceptors(spec: Spec): Result<Map<TestCase, TestResult>> {85 val interceptors = listOfNotNull(86 if (platform == Platform.JS) IgnoreNestedSpecStylesInterceptor(listener, context.configuration.registry) else null,87 ProjectContextInterceptor(context.toProjectContext()),88 SpecExtensionInterceptor(context.configuration.registry),89 ConfigurationInContextInterceptor(context.configuration),90 )91 val initial: suspend (Spec) -> Result<Map<TestCase, TestResult>> = {92 try {93 val delegate = createSpecExecutorDelegate(listener, defaultCoroutineDispatcherFactory, context.configuration)94 logger.log { Pair(spec::class.bestName(), "delegate=$delegate") }95 Result.success(delegate.execute(spec))96 } catch (t: Throwable) {97 logger.log { Pair(spec::class.bestName(), "Error executing spec $t") }98 Result.failure(t)99 }100 }101 logger.log { Pair(spec::class.bestName(), "Executing ${interceptors.size} spec interceptors") }102 return interceptors.foldRight(initial) { ext, fn ->103 { spec -> ext.intercept(spec, fn) }104 }.invoke(spec)105 }106 /**107 * Creates an instance of the given [SpecRef], notifies users of the instantiation event108 * or instantiation failure, and returns a Result with the error or spec.109 *110 * After this method is called the spec is sealed.111 */112 private suspend fun createInstance(ref: SpecRef): Result<Spec> =113 ref.instance(context.configuration.registry)114 .onFailure { extensions.specInstantiationError(ref.kclass, it) }115 .flatMap { spec -> extensions.specInstantiated(spec).map { spec } }116 .onSuccess { if (it is DslDrivenSpec) it.seal() }117}118interface SpecExecutorDelegate {119 suspend fun execute(spec: Spec): Map<TestCase, TestResult>120}121@ExperimentalKotest122internal expect fun createSpecExecutorDelegate(123 listener: TestEngineListener,124 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,125 configuration: ProjectConfiguration,126): SpecExecutorDelegate...

Full Screen

Full Screen

AfterTestExceptionTest.kt

Source:AfterTestExceptionTest.kt Github

copy

Full Screen

...17import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory18import io.kotest.engine.extensions.ExtensionException19import io.kotest.engine.interceptors.EngineContext20import io.kotest.engine.listener.AbstractTestEngineListener21import io.kotest.engine.spec.SpecExecutor22import io.kotest.matchers.throwable.shouldHaveMessage23import io.kotest.matchers.types.shouldBeInstanceOf24private class BehaviorSpecWithAfterTestError : BehaviorSpec({25 isolationMode = IsolationMode.InstancePerTest26 afterTest {27 error("boom")28 }29 given("given") {30 When("when") {31 then("then") {32 }33 }34 }35})36private class FunSpecWithAfterTestError : FunSpec({37 isolationMode = IsolationMode.InstancePerTest38 afterTest {39 error("boom")40 }41 test("fun spec") {}42})43private class StringSpecWithAfterTestError : StringSpec({44 isolationMode = IsolationMode.InstancePerTest45 afterTest {46 error("boom")47 }48 "string test"{}49})50private class ShouldSpecWithAfterTestError : ShouldSpec({51 isolationMode = IsolationMode.InstancePerTest52 afterTest {53 error("boom")54 }55 should("foo") {}56})57private class DescribeSpecWithAfterTestError : DescribeSpec({58 isolationMode = IsolationMode.InstancePerTest59 afterTest {60 error("boom")61 }62})63private class FeatureSpecWithAfterTestError : FeatureSpec({64 isolationMode = IsolationMode.InstancePerTest65 afterTest {66 error("boom")67 }68 feature("feature") {69 scenario("scenario") { }70 }71})72private class ExpectSpecWithAfterTestError : ExpectSpec({73 isolationMode = IsolationMode.InstancePerTest74 afterTest {75 error("boom")76 }77})78private class FreeSpecWithAfterTestError : FreeSpec({79 isolationMode = IsolationMode.InstancePerTest80 afterTest {81 error("boom")82 }83 "test" {}84})85private class WordSpecWithAfterTestError : WordSpec({86 isolationMode = IsolationMode.InstancePerTest87 afterTest {88 error("boom")89 }90 "this test" should {91 "be alive" {}92 }93})94@ExperimentalKotest95class AfterTestExceptionTest : WordSpec({96 var error: Throwable? = null97 val listener = object : AbstractTestEngineListener() {98 override suspend fun testFinished(testCase: TestCase, result: TestResult) {99 if (result is TestResult.Error)100 error = result.cause101 }102 }103 "an exception in before test" should {104 "fail the test for behavior spec" {105 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))106 executor.execute(BehaviorSpecWithAfterTestError::class)107 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()108 error!!.cause!!.shouldHaveMessage("boom")109 }110 "fail the test for feature spec" {111 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))112 executor.execute(FeatureSpecWithAfterTestError::class)113 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()114 error!!.cause!!.shouldHaveMessage("boom")115 }116 "fail the test for word spec" {117 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))118 executor.execute(WordSpecWithAfterTestError::class)119 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()120 error!!.cause!!.shouldHaveMessage("boom")121 }122 "fail the test for should spec" {123 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))124 executor.execute(ShouldSpecWithAfterTestError::class)125 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()126 error!!.cause!!.shouldHaveMessage("boom")127 }128 "fail the test for string spec" {129 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))130 executor.execute(StringSpecWithAfterTestError::class)131 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()132 error!!.cause!!.shouldHaveMessage("boom")133 }134 "fail the test for describe spec" {135 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))136 executor.execute(DescribeSpecWithAfterTestError::class)137 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()138 error!!.cause!!.shouldHaveMessage("boom")139 }140 "fail the test for free spec" {141 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))142 executor.execute(FreeSpecWithAfterTestError::class)143 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()144 error!!.cause!!.shouldHaveMessage("boom")145 }146 "fail the test for fun spec" {147 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))148 executor.execute(FunSpecWithAfterTestError::class)149 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()150 error!!.cause!!.shouldHaveMessage("boom")151 }152 "fail the test for expect spec" {153 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))154 executor.execute(ExpectSpecWithAfterTestError::class)155 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()156 error!!.cause!!.shouldHaveMessage("boom")157 }158 }159})...

Full Screen

Full Screen

BeforeTestExceptionTest.kt

Source:BeforeTestExceptionTest.kt Github

copy

Full Screen

...16import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory17import io.kotest.engine.extensions.ExtensionException18import io.kotest.engine.interceptors.EngineContext19import io.kotest.engine.listener.AbstractTestEngineListener20import io.kotest.engine.spec.SpecExecutor21import io.kotest.matchers.throwable.shouldHaveMessage22import io.kotest.matchers.types.shouldBeInstanceOf23private class BehaviorSpecWithBeforeTestError : BehaviorSpec({24 isolationMode = IsolationMode.InstancePerTest25 beforeTest {26 error("boom")27 }28 given("given") {29 When("when") {30 then("then") {31 }32 }33 }34})35private class FunSpecWithBeforeTestError : FunSpec({36 isolationMode = IsolationMode.InstancePerTest37 beforeTest {38 error("boom")39 }40 test("fun spec") {}41})42private class StringSpecWithBeforeTestError : StringSpec({43 isolationMode = IsolationMode.InstancePerTest44 beforeTest {45 error("boom")46 }47 "string test"{}48})49private class ShouldSpecWithBeforeTestError : ShouldSpec({50 isolationMode = IsolationMode.InstancePerTest51 beforeTest {52 error("boom")53 }54 should("foo") {}55})56private class DescribeSpecWithBeforeTestError : DescribeSpec({57 isolationMode = IsolationMode.InstancePerTest58 beforeTest {59 error("boom")60 }61})62private class FeatureSpecWithBeforeTestError : FeatureSpec({63 isolationMode = IsolationMode.InstancePerTest64 beforeTest {65 error("boom")66 }67 feature("feature") {68 scenario("scenario") { }69 }70})71private class ExpectSpecWithBeforeTestError : ExpectSpec({72 isolationMode = IsolationMode.InstancePerTest73 beforeTest {74 error("boom")75 }76})77private class FreeSpecWithBeforeTestError : FreeSpec({78 isolationMode = IsolationMode.InstancePerTest79 beforeTest {80 error("boom")81 }82 "test" {}83})84private class WordSpecWithBeforeTestError : WordSpec({85 isolationMode = IsolationMode.InstancePerTest86 beforeTest {87 error("boom")88 }89 "this test" should {90 "be alive" {}91 }92})93@ExperimentalKotest94class BeforeTestExceptionTest : WordSpec({95 var error: Throwable? = null96 val listener = object : AbstractTestEngineListener() {97 override suspend fun testFinished(testCase: TestCase, result: TestResult) {98 if (result.isError)99 error = result.errorOrNull100 }101 }102 "an exception in before test" should {103 "fail the test for behavior spec" {104 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))105 executor.execute(BehaviorSpecWithBeforeTestError::class)106 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()107 error!!.cause!!.shouldHaveMessage("boom")108 }109 "fail the test for feature spec" {110 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))111 executor.execute(FeatureSpecWithBeforeTestError::class)112 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()113 error!!.cause!!.shouldHaveMessage("boom")114 }115 "fail the test for word spec" {116 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))117 executor.execute(WordSpecWithBeforeTestError::class)118 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()119 error!!.cause!!.shouldHaveMessage("boom")120 }121 "fail the test for should spec" {122 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))123 executor.execute(ShouldSpecWithBeforeTestError::class)124 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()125 error!!.cause!!.shouldHaveMessage("boom")126 }127 "fail the test for string spec" {128 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))129 executor.execute(StringSpecWithBeforeTestError::class)130 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()131 error!!.cause!!.shouldHaveMessage("boom")132 }133 "fail the test for describe spec" {134 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))135 executor.execute(DescribeSpecWithBeforeTestError::class)136 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()137 error!!.cause!!.shouldHaveMessage("boom")138 }139 "fail the test for free spec" {140 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))141 executor.execute(FreeSpecWithBeforeTestError::class)142 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()143 error!!.cause!!.shouldHaveMessage("boom")144 }145 "fail the test for fun spec" {146 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))147 executor.execute(FunSpecWithBeforeTestError::class)148 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()149 error!!.cause!!.shouldHaveMessage("boom")150 }151 "fail the test for expect spec" {152 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))153 executor.execute(ExpectSpecWithBeforeTestError::class)154 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()155 error!!.cause!!.shouldHaveMessage("boom")156 }157 }158})...

Full Screen

Full Screen

InitializerExceptionTest.kt

Source:InitializerExceptionTest.kt Github

copy

Full Screen

...13import io.kotest.core.test.TestResult14import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory15import io.kotest.engine.interceptors.EngineContext16import io.kotest.engine.listener.AbstractTestEngineListener17import io.kotest.engine.spec.SpecExecutor18import io.kotest.engine.spec.SpecInstantiationException19import io.kotest.matchers.types.shouldBeInstanceOf20import kotlin.reflect.KClass21private class BehaviorSpecWithInitError : BehaviorSpec() {22 override fun isolationMode() = IsolationMode.InstancePerTest23 init {24 error("boom")25 }26}27private class FunSpecWithInitError : FunSpec() {28 override fun isolationMode() = IsolationMode.InstancePerTest29 init {30 error("boom")31 }32}33private class StringSpecWithInitError : StringSpec() {34 override fun isolationMode() = IsolationMode.InstancePerTest35 init {36 error("boom")37 }38}39private class ShouldSpecWithInitError : ShouldSpec() {40 override fun isolationMode() = IsolationMode.InstancePerTest41 init {42 error("boom")43 }44}45private class DescribeSpecWithInitError : DescribeSpec() {46 override fun isolationMode() = IsolationMode.InstancePerTest47 init {48 error("boom")49 }50}51private class FeatureSpecWithInitError : FeatureSpec() {52 override fun isolationMode() = IsolationMode.InstancePerTest53 init {54 error("boom")55 }56}57private class ExpectSpecWithInitError : ExpectSpec() {58 override fun isolationMode() = IsolationMode.InstancePerTest59 init {60 error("boom")61 }62}63private class FreeSpecWithInitError : FreeSpec() {64 override fun isolationMode() = IsolationMode.InstancePerTest65 init {66 error("boom")67 }68}69private class WordSpecWithInitError : WordSpec() {70 override fun isolationMode() = IsolationMode.InstancePerTest71 init {72 error("boom")73 }74}75class InitializerExceptionTest : WordSpec({76 var error: Throwable? = null77 val listener = object : AbstractTestEngineListener() {78 override suspend fun specFinished(kclass: KClass<*>, result: TestResult) {79 result.errorOrNull?.let { error = it }80 }81 }82 "an exception in the initializer" should {83 "fail the test for behavior spec" {84 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))85 executor.execute(BehaviorSpecWithInitError::class)86 error.shouldBeInstanceOf<SpecInstantiationException>()87 }88 "fail the test for feature spec" {89 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))90 executor.execute(FeatureSpecWithInitError::class)91 error.shouldBeInstanceOf<SpecInstantiationException>()92 }93 "fail the test for word spec" {94 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))95 executor.execute(WordSpecWithInitError::class)96 error.shouldBeInstanceOf<SpecInstantiationException>()97 }98 "fail the test for should spec" {99 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))100 executor.execute(ShouldSpecWithInitError::class)101 error.shouldBeInstanceOf<SpecInstantiationException>()102 }103 "fail the test for string spec" {104 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))105 executor.execute(StringSpecWithInitError::class)106 error.shouldBeInstanceOf<SpecInstantiationException>()107 }108 "fail the test for describe spec" {109 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))110 executor.execute(DescribeSpecWithInitError::class)111 error.shouldBeInstanceOf<SpecInstantiationException>()112 }113 "fail the test for free spec" {114 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))115 executor.execute(FreeSpecWithInitError::class)116 error.shouldBeInstanceOf<SpecInstantiationException>()117 }118 "fail the test for fun spec" {119 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))120 executor.execute(FunSpecWithInitError::class)121 error.shouldBeInstanceOf<SpecInstantiationException>()122 }123 "fail the test for expect spec" {124 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).withListener(listener))125 executor.execute(ExpectSpecWithInitError::class)126 error.shouldBeInstanceOf<SpecInstantiationException>()127 }128 }129})...

Full Screen

Full Screen

SpecInitializationErrorTest.kt

Source:SpecInitializationErrorTest.kt Github

copy

Full Screen

...4import io.kotest.core.spec.SpecRef5import io.kotest.core.spec.style.FunSpec6import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory7import io.kotest.engine.interceptors.EngineContext8import io.kotest.engine.spec.SpecExecutor9import io.kotest.matchers.shouldBe10import io.kotest.runner.junit.platform.JUnitTestEngineListener11import io.kotest.runner.junit.platform.KotestEngineDescriptor12import org.junit.platform.engine.EngineExecutionListener13import org.junit.platform.engine.TestDescriptor14import org.junit.platform.engine.TestExecutionResult15import org.junit.platform.engine.UniqueId16import org.junit.platform.engine.reporting.ReportEntry17@ExperimentalKotest18class SpecInitializationErrorTest : FunSpec({19 test("an error in a class field should fail spec") {20 val root = KotestEngineDescriptor(21 UniqueId.forEngine("kotest"),22 emptyList(),23 emptyList(),24 emptyList(),25 null,26 )27 val finished = mutableMapOf<String, TestExecutionResult.Status>()28 val engineListener = object : EngineExecutionListener {29 override fun executionFinished(testDescriptor: TestDescriptor, testExecutionResult: TestExecutionResult) {30 finished[testDescriptor.displayName] = testExecutionResult.status31 }32 override fun reportingEntryPublished(testDescriptor: TestDescriptor?, entry: ReportEntry?) {}33 override fun executionSkipped(testDescriptor: TestDescriptor?, reason: String?) {}34 override fun executionStarted(testDescriptor: TestDescriptor?) {}35 override fun dynamicTestRegistered(testDescriptor: TestDescriptor?) {}36 }37 val listener = JUnitTestEngineListener(engineListener, root)38 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).mergeListener(listener))39 executor.execute(SpecRef.Reference(SpecWithFieldError::class))40 finished.toMap() shouldBe mapOf(41 "SpecInstantiationException" to TestExecutionResult.Status.FAILED,42 "com.sksamuel.kotest.runner.junit5.SpecWithFieldError" to TestExecutionResult.Status.FAILED43 )44 }45 test("an error in a class initializer should fail spec") {46 val root = KotestEngineDescriptor(47 UniqueId.forEngine("kotest"),48 emptyList(),49 emptyList(),50 emptyList(),51 null,52 )53 val finished = mutableMapOf<String, TestExecutionResult.Status>()54 val engineListener = object : EngineExecutionListener {55 override fun executionFinished(testDescriptor: TestDescriptor, testExecutionResult: TestExecutionResult) {56 finished[testDescriptor.displayName] = testExecutionResult.status57 }58 override fun reportingEntryPublished(testDescriptor: TestDescriptor?, entry: ReportEntry?) {}59 override fun executionSkipped(testDescriptor: TestDescriptor?, reason: String?) {}60 override fun executionStarted(testDescriptor: TestDescriptor?) {}61 override fun dynamicTestRegistered(testDescriptor: TestDescriptor?) {}62 }63 val listener = JUnitTestEngineListener(engineListener, root)64 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, EngineContext(ProjectConfiguration()).mergeListener(listener))65 executor.execute(SpecRef.Reference(SpecWithInitError::class))66 finished.toMap() shouldBe mapOf(67 "SpecInstantiationException" to TestExecutionResult.Status.FAILED,68 "com.sksamuel.kotest.runner.junit5.SpecWithInitError" to TestExecutionResult.Status.FAILED,69 )70 }71})72private class SpecWithFieldError : FunSpec() {73 private val err = "failme".apply { error("foo") }74 init {75 test("foo") {76 }77 }78}...

Full Screen

Full Screen

ConcurrentTestSuiteScheduler.kt

Source:ConcurrentTestSuiteScheduler.kt Github

copy

Full Screen

...7import io.kotest.engine.concurrency.defaultCoroutineDispatcherFactory8import io.kotest.engine.concurrency.isIsolate9import io.kotest.engine.interceptors.EngineContext10import io.kotest.engine.listener.CollectingTestEngineListener11import io.kotest.engine.spec.SpecExecutor12import io.kotest.mpp.Logger13import io.kotest.mpp.bestName14import kotlinx.coroutines.coroutineScope15import kotlinx.coroutines.launch16import kotlinx.coroutines.sync.Semaphore17import kotlinx.coroutines.sync.withPermit18/**19 * A [TestSuiteScheduler] that schedules specs concurrently, up to a provided [maxConcurrent] value.20 * If the value is 1 then this scheduler will execute specs strictly sequentially.21 *22 * Additionally, on JVM targets, it will recognize the [Isolate] and [DoNotParallelize]23 * annotations to ensure those specs are never scheduled concurrently.24 *25 * @param maxConcurrent The maximum number of concurrent coroutines.26 */27@ExperimentalKotest28internal class ConcurrentTestSuiteScheduler(29 private val maxConcurrent: Int,30 private val context: EngineContext,31) : TestSuiteScheduler {32 private val logger = Logger(ConcurrentTestSuiteScheduler::class)33 override suspend fun schedule(suite: TestSuite): EngineResult {34 logger.log { Pair(null, "Launching ${suite.specs.size} specs") }35 val (sequential, concurrent) = suite.specs.partition { it.kclass.isIsolate() }36 logger.log { Pair(null, "Split on isIsolate: ${sequential.size} sequential ${concurrent.size} concurrent") }37 schedule(concurrent, maxConcurrent)38 logger.log { Pair(null, "Concurrent specs have completed") }39 schedule(sequential, 1)40 logger.log { Pair(null, "Sequential specs have completed") }41 return EngineResult(emptyList())42 }43 private suspend fun schedule(44 specs: List<SpecRef>,45 concurrency: Int,46 ) = coroutineScope { // we don't want this function to return until all specs are completed47 val coroutineDispatcherFactory = defaultCoroutineDispatcherFactory(context.configuration)48 val semaphore = Semaphore(concurrency)49 val collector = CollectingTestEngineListener()50 specs.forEach { ref ->51 logger.log { Pair(ref.kclass.bestName(), "Scheduling coroutine") }52 launch {53 semaphore.withPermit {54 logger.log { Pair(ref.kclass.bestName(), "Acquired permit") }55 if (context.configuration.projectWideFailFast && collector.errors) {56 context.listener.specIgnored(ref.kclass, null)57 } else {58 try {59 val executor = SpecExecutor(coroutineDispatcherFactory, context.mergeListener(collector))60 logger.log { Pair(ref.kclass.bestName(), "Executing ref") }61 executor.execute(ref)62 } catch (t: Throwable) {63 logger.log { Pair(ref.kclass.bestName(), "Unhandled error during spec execution $t") }64 throw t65 }66 }67 }68 logger.log { Pair(ref.kclass.bestName(), "Released permit") }69 }70 }71 }72}...

Full Screen

Full Screen

TestSuiteScheduler.kt

Source:TestSuiteScheduler.kt Github

copy

Full Screen

...3import io.kotest.core.project.TestSuite4import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory5import io.kotest.engine.interceptors.EngineContext6import io.kotest.engine.listener.CollectingTestEngineListener7import io.kotest.engine.spec.SpecExecutor8import io.kotest.mpp.Logger9/**10 * A [TestSuiteScheduler] is responsible for launching each spec from a [TestSuite] into a coroutine.11 */12@ExperimentalKotest13internal interface TestSuiteScheduler {14 suspend fun schedule(15 suite: TestSuite,16 ): EngineResult17}18/**19 * A [TestSuiteScheduler] that launches specs sequentially in a loop.20 */21@ExperimentalKotest22internal class SequentialTestSuiteScheduler(23 private val context: EngineContext24) : TestSuiteScheduler {25 private val logger = Logger(this::class)26 override suspend fun schedule(27 suite: TestSuite,28 ): EngineResult {29 logger.log { Pair(null, "Executing ${suite.specs} specs") }30 val collector = CollectingTestEngineListener()31 val errors = mutableListOf<Throwable>()32 suite.specs.forEach {33 try {34 if (context.configuration.projectWideFailFast && collector.errors) {35 context.listener.specIgnored(it.kclass, null)36 } else {37 val executor = SpecExecutor(NoopCoroutineDispatcherFactory, context.mergeListener(collector))38 executor.execute(it)39 }40 } catch (e:Throwable) {41 println(e)42 errors.add(e)43 }44 }45 return EngineResult(errors.toList())46 }47}...

Full Screen

Full Screen

SpecExecutor

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.spec.style.StringSpec2import io.kotest.engine.spec.SpecExecutor3import io.kotest.engine.spec.SpecExecutorListener4import io.kotest.engine.spec.SpecExecutorContext5import io.kotest.core.spec.Spec6import io.kotest.core.test.TestCase7import io.kotest.core.test.TestResult8class MySpec : StringSpec() {9init {10"test" {11}12}13}14val executor = SpecExecutor(MySpec::class)15executor.execute(object: SpecExecutorListener {16override fun specInstantiated(spec: Spec) {17println("spec instantiated")18}19override fun specInstantiated(spec: Spec, context: SpecExecutorContext) {20println("spec instantiated with context")21}22override fun specInstantiated(spec: Spec, context: SpecExecutorContext, result: TestResult) {23println("spec instantiated with context and result")24}25override fun specInstantiated(spec: Spec, result: TestResult) {26println("spec instantiated with result")27}28override fun specInstantiated(spec: Spec, context: SpecExecutorContext, testCase: TestCase, result: TestResult) {29println("spec instantiated with context, test case and result")30}31override fun specInstantiated(spec: Spec, testCase: TestCase, result: TestResult) {32println("spec instantiated with test case and result")33}34override fun specInstantiated(spec: Spec, testCase: TestCase) {35println("spec instantiated with test case")36}37})

Full Screen

Full Screen

SpecExecutor

Using AI Code Generation

copy

Full Screen

1val spec = object : FunSpec({ 2test(“test case”) { 3} 4}) 5SpecExecutor.execute(spec)6val spec = object : FunSpec({ 7test(“test case”) { 8} 9}) 10SpecExecutor.execute(spec)11val spec = object : FunSpec({ 12test(“test case”) { 13} 14}) 15SpecExecutor.execute(spec)16val spec = object : FunSpec({ 17test(“test case”) { 18} 19}) 20SpecExecutor.execute(spec)21val spec = object : FunSpec({ 22test(“test case”) { 23} 24}) 25SpecExecutor.execute(spec)26val spec = object : FunSpec({ 27test(“test case”) { 28} 29}) 30SpecExecutor.execute(spec)31val spec = object : FunSpec({ 32test(“test case”) { 33} 34}) 35SpecExecutor.execute(spec)36val spec = object : FunSpec({ 37test(“test case”) { 38} 39}) 40SpecExecutor.execute(spec)41val spec = object : FunSpec({ 42test(“test case”) { 43} 44}) 45SpecExecutor.execute(spec)46val spec = object : FunSpec({ 47test(“test case”) { 48} 49})

Full Screen

Full Screen

SpecExecutor

Using AI Code Generation

copy

Full Screen

1 val specExecutor = SpecExecutor(spec)2 specExecutor.execute()3 specExecutor.result().tests.forEach{ test ->4 println("${test.name} ${test.status}")5 }6 val spec = SampleTest()7 val specExecutor = SpecExecutor(spec)8 specExecutor.execute()9 val spec = SampleTest()10 val specExecutor = SpecExecutor(spec)11 specExecutor.execute()12 specExecutor.result().tests.forEach{ test ->13 println("${test.name} ${test.status}")14 }

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