Best Kotest code snippet using io.kotest.engine.spec.FailureFirstSorter
WriteFailuresInterceptor.kt
Source:WriteFailuresInterceptor.kt
...7import java.nio.file.Files8import java.nio.file.Paths9import kotlin.reflect.KClass10/**11 * Writes failed specs to a file so that the [io.kotest.engine.spec.FailureFirstSorter]12 * can use the file to run failed specs first.13 *14 * Note: This is a JVM only feature.15 */16@OptIn(KotestInternal::class)17@JVMOnly18internal object WriteFailuresInterceptor : EngineInterceptor {19 override suspend fun intercept(20 context: EngineContext,21 execute: suspend (EngineContext) -> EngineResult22 ): EngineResult {23 return if (context.configuration.writeSpecFailureFile) {24 val collector = CollectingTestEngineListener()25 val result = execute(context.mergeListener(collector))...
sorters.kt
Source:sorters.kt
...3import io.kotest.core.spec.Spec4import io.kotest.mpp.annotation5import java.nio.file.Paths6import kotlin.reflect.KClass7actual class FailureFirstSorter : SpecSorter {8 private val path = Paths.get(".kotest").resolve("spec_failures")9 override fun compare(a: KClass<out Spec>, b: KClass<out Spec>): Int {10 // try to locate a folder called .kotest which should contain a file called spec_failures11 // each line in this file is a failed spec and they should run first12 // if the file doesn't exist then we just execute in Lexico order13 return if (path.toFile().exists()) {14 val classnames = path.toFile().readLines()15 val afailed = classnames.contains(a.qualifiedName)16 val bfailed = classnames.contains(b.qualifiedName)17 return when {18 afailed && bfailed -> LexicographicSpecSorter.compare(a, b)19 afailed -> -120 bfailed -> 121 else -> LexicographicSpecSorter.compare(a, b)...
ordering.kt
Source:ordering.kt
...16 return when (order) {17 SpecExecutionOrder.Undefined -> specs18 SpecExecutionOrder.Lexicographic -> LexicographicSpecSorter.sort(specs)19 SpecExecutionOrder.Annotated -> AnnotatedSpecSorter.sort(specs)20 SpecExecutionOrder.FailureFirst -> FailureFirstSorter().sort(specs)21 SpecExecutionOrder.Random -> {22 val random = when (val seed = configuration.randomOrderSeed) {23 null -> Random.Default24 else -> Random(seed)25 }26 RandomSpecSorter(random).sort(specs)27 }28 }29 }30}
FailureFirstSorter.kt
Source:FailureFirstSorter.kt
1package io.kotest.engine.spec2import io.kotest.core.spec.Spec3import kotlin.reflect.KClass4actual class FailureFirstSorter : SpecSorter {5 override fun compare(a: KClass<out Spec>, b: KClass<out Spec>): Int = 06}7/**8 * An implementation of [SpecExecutionOrder] which will order specs based on the integer9 * value of the [Order] annotation. If the annotation is not present, then that spec is10 * assumed to have a [Int.MAX_VALUE] default value.11 *12 * Note: Runtime annotations are not supported on Native or JS so on those platforms13 * this sort order is a no-op.14 */15actual object AnnotatedSpecSorter : SpecSorter {16 override fun compare(a: KClass<out Spec>, b: KClass<out Spec>): Int = 017}...
failureFirstSort.kt
Source:failureFirstSort.kt
1package io.kotest.engine.spec2import io.kotest.core.spec.Spec3import kotlin.reflect.KClass4actual class FailureFirstSorter : SpecSorter {5 override fun compare(a: KClass<out Spec>, b: KClass<out Spec>): Int = 06}7actual object AnnotatedSpecSorter : SpecSorter {8 override fun compare(a: KClass<out Spec>, b: KClass<out Spec>): Int = 09}...
FailureFirstSorter
Using AI Code Generation
1import io.kotest.core.spec.style.StringSpec2import io.kotest.engine.spec.FailureFirstSorter3import io.kotest.engine.spec.SpecSorter4import io.kotest.engine.spec.SpecSorterExtension5class FailureFirstSorterSpec : StringSpec() {6 override fun specSorter(): SpecSorter? = FailureFirstSorter7 override fun extensions(): List<SpecSorterExtension> = listOf(SpecSorterExtension)8 init {9 "this test should fail" {10 error("boom")11 }12 "this test should pass" {13 }14 }15}16import io.kotest.core.spec.style.StringSpec17import io.kotest.core.spec.SpecSorter18import io.kotest.core.spec.SpecSorterExtension19import io.kotest.core.spec.FailureFirstSorter20class FailureFirstSorterSpec : StringSpec() {21 override fun specSorter(): SpecSorter? = FailureFirstSorter22 override fun extensions(): List<SpecSorterExtension> = listOf(SpecSorterExtension)23 init {24 "this test should fail" {25 error("boom")26 }27 "this test should pass" {28 }29 }30}31 at io.kotest.engine.spec.FailureFirstSorterSpec$1.invokeSuspend(FailureFirstSorterSpec.kt:18)32 at io.kotest.engine.spec.FailureFirstSorterSpec$1.invoke(FailureFirstSorterSpec.kt)33 at io.kotest.engine.spec.FailureFirstSorterSpec$1.invoke(FailureFirstSorterSpec.kt:10)34 at io.kotest.engine.spec.FailureFirstSorterSpec$1.invoke(FailureFirstSorterSpec.kt:10)35 at io.kotest.core.spec.style.StringSpec$default$1$1.invokeSuspend(StringSpec.kt:70)36 at io.kotest.core.spec.style.StringSpec$default$1$1.invoke(StringSpec.kt)37 at io.kotest.core.spec.style.StringSpec$default$1$1.invoke(StringSpec.kt:54)
FailureFirstSorter
Using AI Code Generation
1config { 2}3config { 4}5config { 6}7config { 8}9config { 10}11config { 12}13config { 14}15config { 16}17config { 18}19config { 20}21config { 22}23config { 24}
FailureFirstSorter
Using AI Code Generation
1 FailureFirstSorter.sort(listOf("test 1", "test 2", "test 3"))2 FailureFirstSorter.sort(listOf("test 1", "test 2", "test 3"))3 FailureFirstSorter.sort(listOf("test 1", "test 2", "test 3"))4 FailureFirstSorter.sort(listOf("test 1", "test 2", "test 3"))5 FailureFirstSorter.sort(listOf("test 1", "test 2", "test 3"))6 FailureFirstSorter.sort(listOf("test 1", "test 2", "test 3"))7 FailureFirstSorter.sort(listOf("test 1", "test 2", "test 3"))8 FailureFirstSorter.sort(listOf("test 1", "test 2", "test 3"))9 FailureFirstSorter.sort(listOf("test 1", "test 2", "test 3"))
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!!