Best Kotest code snippet using io.kotest.engine.spec.SpecExtensions
SingleInstanceSpecRunner.kt
Source:SingleInstanceSpecRunner.kt
...9import io.kotest.core.test.TestResult10import io.kotest.core.test.TestScope11import io.kotest.engine.listener.TestEngineListener12import 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,...
RequiresTagSpecInterceptor.kt
Source:RequiresTagSpecInterceptor.kt
...9import io.kotest.core.spec.SpecRef10import io.kotest.core.test.TestCase11import io.kotest.core.test.TestResult12import io.kotest.engine.listener.TestEngineListener13import io.kotest.engine.spec.SpecExtensions14import io.kotest.engine.tags.isActive15import io.kotest.engine.tags.parse16import io.kotest.engine.tags.runtimeTags17import io.kotest.mpp.annotation18/**19 * A [SpecFilter] which will ignore specs if they are annotated with @[RequiresTag]20 * and those tags are not present in the runtime tags.21 */22internal class RequiresTagSpecInterceptor(23 private val listener: TestEngineListener,24 private val configuration: ProjectConfiguration,25 private val registry: ExtensionRegistry,26) : SpecRefInterceptor {27 override suspend fun intercept(28 ref: SpecRef,29 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>30 ): Result<Map<TestCase, TestResult>> {31 return when (val annotation = ref.kclass.annotation<RequiresTag>()) {32 null -> fn(ref)33 else -> {34 val requiredTags = annotation.wrapper.map { NamedTag(it) }.toSet()35 val expr = configuration.runtimeTags().parse()36 if (requiredTags.isEmpty() || expr.isActive(requiredTags)) {37 fn(ref)38 } else {39 runCatching { listener.specIgnored(ref.kclass, "Disabled by @RequiresTag") }40 .flatMap { SpecExtensions(registry).ignored(ref.kclass, "Disabled by @RequiresTag") }41 .map { emptyMap() }42 }43 }44 }45 }46}...
IgnoreNestedSpecStylesInterceptor.kt
Source:IgnoreNestedSpecStylesInterceptor.kt
...9import io.kotest.core.spec.style.StringSpec10import io.kotest.core.test.TestCase11import io.kotest.core.test.TestResult12import io.kotest.engine.listener.TestEngineListener13import io.kotest.engine.spec.SpecExtensions14import io.kotest.mpp.bestName15import io.kotest.mpp.log16/**17 * Filters [Spec]'s that are not compatible on platforms that disallow nested tests.18 */19internal class IgnoreNestedSpecStylesInterceptor(20 private val listener: TestEngineListener,21 registry: ExtensionRegistry,22) : SpecInterceptor {23 private val extensions = SpecExtensions(registry)24 override suspend fun intercept(25 spec: Spec,26 fn: suspend (Spec) -> Result<Map<TestCase, TestResult>>27 ): Result<Map<TestCase, TestResult>> {28 fun isValid(spec: Spec) = when (spec) {29 is FunSpec, is ExpectSpec, is FeatureSpec, is ShouldSpec, is StringSpec -> true30 else -> false31 }32 return if (isValid(spec)) {33 fn(spec)34 } else {35 log { "IgnoreNestedSpecStylesInterceptor: Marking ${spec::class.bestName()} as inactive due to platform limitations" }36 println("WARN: kotest-js only supports top level tests due to underlying platform limitations. '${spec::class.bestName()}' has been marked as ignored")37 runCatching { listener.specIgnored(spec::class, "Disabled due to platform limitations") }...
EnabledIfSpecInterceptor.kt
Source:EnabledIfSpecInterceptor.kt
...6import io.kotest.core.spec.SpecRef7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestResult9import io.kotest.engine.listener.TestEngineListener10import io.kotest.engine.spec.SpecExtensions11import io.kotest.mpp.annotation12import io.kotest.mpp.newInstanceNoArgConstructor13/**14 * Evaluates any spec annotated with [EnabledIf] if the condition fails, skips the spec15 * and notifies the [TestEngineListener] that the spec is ignored.16 *17 * Note: annotations are only available on the JVM.18 */19internal class EnabledIfSpecInterceptor(20 private val listener: TestEngineListener,21 registry: ExtensionRegistry,22) : SpecRefInterceptor {23 private val extensions = SpecExtensions(registry)24 override suspend fun intercept(25 ref: SpecRef,26 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>27 ): Result<Map<TestCase, TestResult>> {28 val enabled = ref.kclass29 .annotation<EnabledIf>()30 ?.wrapper31 ?.newInstanceNoArgConstructor()32 ?.enabled(ref.kclass) ?: true33 return if (enabled) {34 fn(ref)35 } else {36 runCatching { listener.specIgnored(ref.kclass, "Disabled by @EnabledIf") }37 .flatMap { extensions.ignored(ref.kclass, "Disabled by @EnabledIf") }...
SpecFilterInterceptor.kt
Source:SpecFilterInterceptor.kt
...5import io.kotest.core.spec.SpecRef6import io.kotest.core.test.TestCase7import io.kotest.core.test.TestResult8import io.kotest.engine.listener.TestEngineListener9import io.kotest.engine.spec.SpecExtensions10import io.kotest.mpp.Logger11import io.kotest.mpp.bestName12/**13 * Evaluates a spec against any registered [SpecFilter]s.14 */15class SpecFilterInterceptor(16 private val listener: TestEngineListener,17 private val registry: ExtensionRegistry18) : SpecRefInterceptor {19 private val extensions = SpecExtensions(registry)20 private val logger = Logger(SpecFilterInterceptor::class)21 override suspend fun intercept(22 ref: SpecRef,23 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>24 ): Result<Map<TestCase, TestResult>> {25 val excluded = registry.all().filterIsInstance<SpecFilter>().mapNotNull {26 val result = it.filter(ref.kclass)27 if (result is SpecFilterResult.Exclude) result else null28 }.firstOrNull()29 logger.log { Pair(ref.kclass.bestName(), "excludedByFilters == $excluded") }30 return if (excluded == null) {31 fn(ref)32 } else {33 val reason = excluded.reason ?: "Disabled by spec filter"...
IgnoredSpecInterceptor.kt
Source:IgnoredSpecInterceptor.kt
...5import io.kotest.core.spec.SpecRef6import io.kotest.core.test.TestCase7import io.kotest.core.test.TestResult8import io.kotest.engine.listener.TestEngineListener9import io.kotest.engine.spec.SpecExtensions10import io.kotest.mpp.Logger11import io.kotest.mpp.bestName12import io.kotest.mpp.hasAnnotation13/**14 * Skips any spec marked with the [Ignored] annotation and notifies the [TestEngineListener]15 * that the spec is ignored.16 *17 * Note: annotations are only available on the JVM.18 */19internal class IgnoredSpecInterceptor(20 private val listener: TestEngineListener,21 registry: ExtensionRegistry,22) : SpecRefInterceptor {23 private val logger = Logger(IgnoredSpecInterceptor::class)24 private val extensions = SpecExtensions(registry)25 override suspend fun intercept(26 ref: SpecRef,27 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>28 ): Result<Map<TestCase, TestResult>> {29 val isIgnored = ref.kclass.hasAnnotation<Ignored>()30 logger.log { Pair(ref.kclass.bestName(), "@Ignored == $isIgnored") }31 return if (isIgnored) {32 runCatching { listener.specIgnored(ref.kclass, "Disabled by @Ignored") }33 .flatMap { extensions.ignored(ref.kclass, "Disabled by @Ignored") }34 .map { emptyMap() }35 } else {36 fn(ref)37 }38 }...
TagsExcludedDiscoveryExtension.kt
Source:TagsExcludedDiscoveryExtension.kt
...6import io.kotest.core.spec.SpecRef7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestResult9import io.kotest.engine.listener.TestEngineListener10import io.kotest.engine.spec.SpecExtensions11import io.kotest.engine.tags.isPotentiallyActive12import io.kotest.engine.tags.parse13import io.kotest.engine.tags.runtimeTags14/**15 * Filters any [Spec] that can be eagerly excluded based on the @[TagExpression] annotation at the class level.16 */17class TagsExcludedSpecInterceptor(18 private val listener: TestEngineListener,19 private val conf: ProjectConfiguration,20) : SpecRefInterceptor {21 private val extensions = SpecExtensions(conf.registry)22 override suspend fun intercept(23 ref: SpecRef,24 fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>>25 ): Result<Map<TestCase, TestResult>> {26 val potentiallyActive = conf.runtimeTags().parse().isPotentiallyActive(ref.kclass)27 return if (potentiallyActive) {28 fn(ref)29 } else {30 runCatching { listener.specIgnored(ref.kclass, null) }31 .flatMap { extensions.ignored(ref.kclass, "Skipped by tags") }32 .map { emptyMap() }33 }34 }35}...
SpecExtensionInterceptor.kt
Source:SpecExtensionInterceptor.kt
...3import io.kotest.core.extensions.SpecExtension4import io.kotest.core.spec.Spec5import io.kotest.core.test.TestCase6import io.kotest.core.test.TestResult7import io.kotest.engine.spec.SpecExtensions8/**9 * A [SpecInterceptor] that executes all [SpecExtension]s.10 */11internal class SpecExtensionInterceptor(registry: ExtensionRegistry) : SpecInterceptor {12 val extensions = SpecExtensions(registry)13 override suspend fun intercept(14 spec: Spec,15 fn: suspend (Spec) -> Result<Map<TestCase, TestResult>>16 ): Result<Map<TestCase, TestResult>> {17 return extensions.intercept(spec) { fn(spec) } ?: Result.success(emptyMap())18 }19}...
SpecExtensions
Using AI Code Generation
1 import io.kotest.engine.spec.SpecExtensions2 import io.kotest.core.spec.style.StringSpec3 import io.kotest.matchers.shouldBe4 class MySpec : StringSpec() {5 init {6 SpecExtensions.register(MyExtension())7 "this test should be ignored" {8 }9 "this test should be executed" {10 }11 }12 }13 import io.kotest.core.extensions.SpecExtension14 import io.kotest.core.spec.Spec15 import io.kotest.core.test.TestCase16 import io.kotest.core.test.TestResult17 import io.kotest.core.test.TestType18 class MyExtension : SpecExtension {19 override suspend fun intercept(20 execute: suspend (TestCase) -> TestResult21 ): TestResult {22 return when (testCase.type) {23 TestType.Container -> execute(testCase)24 TestType.Test -> {25 if (testCase.name.contains("ignore", ignoreCase = true)) {26 } else {27 execute(testCase)28 }29 }30 }31 }32 }
SpecExtensions
Using AI Code Generation
1class MySpec : StringSpec() {2override fun extensions() = listOf(MyExtension())3}4class MySpec : StringSpec() {5override fun extensions() = listOf(MyExtension())6}7class MySpec : StringSpec() {8override fun extensions() = listOf(MyExtension())9}10class MySpec : StringSpec() {11override fun extensions() = listOf(MyExtension())12}13class MySpec : StringSpec() {14override fun extensions() = listOf(MyExtension())15}16class MySpec : StringSpec() {17override fun extensions() = listOf(MyExtension())18}19class MySpec : StringSpec() {20override fun extensions() = listOf(MyExtension())21}22class MySpec : StringSpec() {23override fun extensions() = listOf(MyExtension())24}25class MySpec : StringSpec() {26override fun extensions() = listOf(MyExtension())27}28class MySpec : StringSpec() {29override fun extensions() = listOf(MyExtension())30}31class MySpec : StringSpec() {32override fun extensions() = listOf(MyExtension())33}34class MySpec : StringSpec() {35override fun extensions() = listOf(MyExtension())36}37class MySpec : StringSpec() {38override fun extensions()
SpecExtensions
Using AI Code Generation
1 val specExtensions = SpecExtensions()2 specExtensions.registerExtension(MySpecExtension())3 val executionOrder = SpecExecutionOrder()4 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)5 val executionOrder = SpecExecutionOrder()6 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)7 val executionOrder = SpecExecutionOrder()8 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)9 val executionOrder = SpecExecutionOrder()10 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)11 val executionOrder = SpecExecutionOrder()12 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)13 val executionOrder = SpecExecutionOrder()14 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)15 val executionOrder = SpecExecutionOrder()16 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)17 val executionOrder = SpecExecutionOrder()18 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)19 val executionOrder = SpecExecutionOrder()20 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)21 val executionOrder = SpecExecutionOrder()22 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order.RANDOM)23 val executionOrder = SpecExecutionOrder()24 executionOrder.setOrder(MySpec::class, SpecExecutionOrder.Order
SpecExtensions
Using AI Code Generation
1class MySpec : FunSpec() {2 override fun extensions() = listOf(MySpecExtension())3 init {4 test("test case 1") {5 }6 }7}8class MySpecExtension : SpecExtension {9 override suspend fun intercept(spec: Spec, process: suspend (Spec) -> Unit) {10 process(spec)11 }12}13class MyTestCaseExtension : TestCaseExtension {14 override suspend fun intercept(testCase: TestCase, process: suspend (TestCase) -> Unit) {15 process(testCase)16 }17}18class MyTestExtension : TestExtension {19 override suspend fun intercept(testCase: TestCase, process: suspend (TestCase) -> TestResult) {20 process(testCase)21 }22}23class MyTestListener : TestListener {24 override suspend fun beforeSpec(spec: Spec) {25 }26 override suspend fun afterSpec(spec: Spec) {27 }28 override suspend fun beforeTest(testCase: TestCase) {29 }30 override suspend fun afterTest(testCase: TestCase, result: TestResult) {31 }32}33class MyProjectListener : ProjectListener {34 override suspend fun beforeProject() {35 }36 override suspend fun afterProject() {37 }38}39class MyEngineListener : EngineListener {40 override suspend fun beforeSpec(spec: Spec) {41 }42 override suspend fun afterSpec(spec: Spec) {43 }44 override suspend fun beforeTest(testCase: TestCase) {45 }46 override suspend fun afterTest(testCase: TestCase, result: TestResult) {47 }48 override suspend fun beforeProject() {49 }50 override suspend fun afterProject() {51 }52}53class MySpecExecutionOrderExtension : SpecExecutionOrderExtension {54 override fun order(specs: List<Spec>): List<Spec> {
SpecExtensions
Using AI Code Generation
1 fun test() {2 val spec = object : Spec({3 }) {}4 val extensions = SpecExtensions()5 val engine = TestEngine(listOf(spec), extensions)6 engine.execute()7 }
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!!