Best Kotest code snippet using io.kotest.assertions.clues
ClueTest.kt
Source:ClueTest.kt
...28 withClue("a clue:") { "1" shouldBe withClueEcho("here are the details!") }29 }30 ex.message shouldBe "a clue:\nShould have the details of '1' and here are the details!"31 }32 "should add clues correctly with multiple/softAssert" {33 val ex = shouldThrow<AssertionError> {34 withClue("outer clue:") {35 assertSoftly {36 "1" shouldBe withClueEcho("the details!")37 withClue("inner clue:") { "2" shouldBe "1" }38 }39 }40 }41 ex.message.apply {42 shouldContain("outer clue:\nShould have the details of '1' and the details!")43 shouldContain("inner clue:\nexpected:<\"1\"> but was:<\"2\">")44 }45 }46 "should show all available nested clue contexts" {47 withClue("clue outer:") {48 shouldThrow<AssertionError> { "1" shouldBe "2" }.message shouldBe "clue outer:\nexpected:<\"2\"> but was:<\"1\">"49 withClue("clue inner:") {50 shouldThrow<AssertionError> { "3" shouldBe "4" }.message shouldBe "clue outer:\nclue inner:\nexpected:<\"4\"> but was:<\"3\">"51 }52 shouldThrow<AssertionError> { "5" shouldBe "6" }.message shouldBe "clue outer:\nexpected:<\"6\"> but was:<\"5\">"53 }54 //And resets completely when leaving final clue block55 shouldThrow<AssertionError> { "7" shouldBe "8" }.message shouldBe "expected:<\"8\"> but was:<\"7\">"56 }57 "should only invoke the lazy if an assertion fails" {58 val expected = UUID.randomUUID()59 var state: String? = null60 val clue: Lazy<UUID> = lazy {61 expected.also { state = it.toString() }62 }63 state.shouldBeNull()64 withClue(clue) {65 1 + 1 shouldBe 266 }67 state.shouldBeNull()68 withClue(clue) {69 shouldThrow<AssertionError> { "1" shouldBe "2" }.message shouldStartWith expected.toString()70 }71 state shouldBe expected.toString()72 }73 "clue can be nullable" {74 val ex = shouldThrow<AssertionError> {75 withClue(null) { 1 shouldBe 2 }76 }77 ex.message shouldBe "null\nexpected:<2> but was:<1>"78 }79 }80 "asClue()" - {81 "should prepend clue to message with a newline" {82 val ex = shouldThrow<AssertionError> {83 "a clue:".asClue { "1" shouldBe "2" }84 }85 ex.message shouldBe "a clue:\nexpected:<\"2\"> but was:<\"1\">"86 }87 "should add clues correctly with multiple/softAssert" {88 val ex = shouldThrow<AssertionError> {89 "outer clue:".asClue {90 assertSoftly {91 "1" shouldBe "the details"92 "inner clue:".asClue { "2" shouldBe "1" }93 }94 }95 }96 ex.message.apply {97 shouldContain("outer clue:\nexpected:<\"the details\"> but was:<\"1\">")98 shouldContain("outer clue:\ninner clue:\nexpected:<\"1\"> but was:<\"2\">")99 }100 }101 "should show all available nested clue contexts" {...
AssertionsTest.kt
Source:AssertionsTest.kt
1package com.psg.kotest_example2import io.kotest.assertions.asClue3import io.kotest.assertions.assertSoftly4import io.kotest.assertions.throwables.shouldThrow5import io.kotest.assertions.throwables.shouldThrowAny6import io.kotest.assertions.withClue7import io.kotest.core.spec.style.FreeSpec8import io.kotest.matchers.collections.shouldContainAll9import io.kotest.matchers.comparables.shouldBeGreaterThanOrEqualTo10import io.kotest.matchers.shouldBe11import io.kotest.matchers.shouldNotBe12import io.kotest.matchers.string.*13class AssertionsTest : FreeSpec() {14 init {15 "Matchers" - {16 val testStr = "I am iron man"17 val testNum = 518 val testList = listOf("iron", "bronze", "silver")19 "ì¼ì¹ íëì§" {20 testStr shouldBe "I am iron man"21 }22 "ì¼ì¹ ì íëì§" {23 testStr shouldNotBe "I am silver man"24 }25 "í´ë¹ 문ìì´ë¡ ììíëì§" {26 testStr shouldStartWith "I am"27 }28 "í´ë¹ 문ìì´ì í¬í¨íëì§" {29 testStr shouldContain "iron"30 }31 "리ì¤í¸ìì í´ë¹ 리ì¤í¸ì ê°ë¤ì´ 모ë í¬í¨ëëì§" {32 testList shouldContainAll listOf("iron", "silver")33 }34 "ëì문ì 무ìíê³ ì¼ì¹íëì§" {35 testStr shouldBeEqualIgnoringCase "I AM IRON MAN"36 }37 "ë³´ë¤ í°ê±°ë ê°ìì§" {38 testNum shouldBeGreaterThanOrEqualTo 339 }40 "í´ë¹ 문ìì´ê³¼ 길ì´ê° ê°ìì§" {41 testStr shouldHaveSameLengthAs "I AM SUPERMAN"42 }43 "문ìì´ ê¸¸ì´" {44 testStr shouldHaveLength 1345 }46 "ì¬ë¬ê° ì²´ì´ë" {47 testStr.shouldStartWith("I").shouldHaveLength(13).shouldContainIgnoringCase("IRON")48 }49 }50 "Exception" - {51 "ArithmeticException Exception ë°ìíëì§" {52 val exception = shouldThrow<ArithmeticException> {53 1 / 054 }55 exception.message shouldStartWith ("/ by zero")56 }57 "ì´ë¤ Exceptionì´ë ë°ìíëì§" {58 val exception = shouldThrowAny {59 1 / 060 }61 exception.message shouldStartWith ("/ by zero")62 }63 }64 "Clues" - {65 data class HttpResponse(val status: Int, val body: String)66 val response = HttpResponse(404, "the content")67 "Not Use Clues" {68 response.status shouldBe 20069 response.body shouldBe "the content"70 // ê²°ê³¼: expected:<200> but was:<404>71 }72 "With Clues" {73 withClue("statusë 200ì´ì¬ì¼ ëê³ bodyë 'the content'ì¬ì¼ íë¤") {74 response.status shouldBe 20075 response.body shouldBe "the content"76 }77 // ê²°ê³¼: statusë 200ì´ì¬ì¼ ëê³ bodyë 'the content'ì¬ì¼ íë¤78 }79 "As Clues" {80 response.asClue {81 it.status shouldBe 20082 it.body shouldBe "the content"83 }84 // ê²°ê³¼: HttpResponse(status=404, body=the content)85 }86 }87 "Soft Assertions" - { // assertê° ì¤ê°ì ì¤í¨í´ë ëê¹ì§ ì²´í¬ê° ê°ë¥í¨88 val testStr = "I am iron man"89 val testNum = 590 "Not Soft" {91 testStr shouldBe "IronMan"92 testNum shouldBe 193 // ê²°ê³¼: expected:<"IronMan"> but was:<"I am iron man">94 }95 "Use Soft" {96 assertSoftly {97 testStr shouldBe "IronMan"98 testNum shouldBe 199 }100 // ê²°ê³¼: expected:<"IronMan"> but was:<"I am iron man">101 // expected:<1> but was:<5>102 }103 }104 }105}...
CluesTests.kt
Source:CluesTests.kt
1package io.kotest.assertions2import io.kotest.core.spec.style.FunSpec3import io.kotest.matchers.collections.shouldHaveSize4import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual5import io.kotest.matchers.shouldBe6import kotlinx.coroutines.Dispatchers7import kotlinx.coroutines.ExperimentalCoroutinesApi8import kotlinx.coroutines.coroutineScope9import kotlinx.coroutines.delay10import kotlinx.coroutines.launch11import kotlinx.coroutines.withContext12class CluesTests : FunSpec({13 test("withClue should not fail on coroutine thread switch") {14 withContext(Dispatchers.Unconfined) {15 val threadIds = mutableSetOf<Long>()16 withClue("should not fail") {17 threadIds.add(Thread.currentThread().id)18 delay(10)19 threadIds.add(Thread.currentThread().id)20 }21 threadIds shouldHaveSize 222 }23 }24 test("concurrent withClue invocations should be isolated from each other") {25 @OptIn(ExperimentalCoroutinesApi::class)26 withContext(Dispatchers.IO.limitedParallelism(8)) {27 val repetitionCount = 10028 val parentCoroutineCount = 229 val childCoroutineCount = 1030 val stepCount = 231 // These parameters specify a certain quota of repetitions to succeed in dispatching coroutines to32 // a specified minimum number of threads. This safeguards against subtle differences in dispatcher33 // implementations (how many threads to allocate, how many threads to actually use for coroutines).34 val minimumThreadCount = 235 val minimumThreadRepetitionQuota = repetitionCount / 236 var minimumThreadRepetitionCount = 037 for (repetitionNumber in 1..repetitionCount) {38 val threadIds = mutableSetOf<Long>()39 coroutineScope {40 for (parentCoroutineNumber in 1..parentCoroutineCount) {41 launch {42 val parentClue = "r=$repetitionNumber, p=$parentCoroutineNumber"43 withClue(parentClue) {44 for (childCoroutineNumber in 1..childCoroutineCount) {45 launch {46 for (stepNumber in 1..stepCount) {47 val childClue = "r=$repetitionNumber, c=$childCoroutineNumber, s=$stepNumber"48 withClue(childClue) {49 clueContextAsString() shouldBe "$parentClue\n$childClue\n"50 synchronized(threadIds) { threadIds.add(Thread.currentThread().id) }51 delay(1L)52 clueContextAsString() shouldBe "$parentClue\n$childClue\n"53 synchronized(threadIds) { threadIds.add(Thread.currentThread().id) }54 }55 }56 }57 }58 }59 }60 }61 }62 if (threadIds.size >= minimumThreadCount)63 minimumThreadRepetitionCount++64 }65 minimumThreadRepetitionCount shouldBeGreaterThanOrEqual minimumThreadRepetitionQuota66 }67 }68})...
ErrorCollector.kt
Source:ErrorCollector.kt
...22 fun pushClue(clue: Any)23 fun popClue()24 /**25 * Returns the current clue context.26 * That is all the clues nested to this point.27 */28 fun clueContext(): List<Any>29}30open class BasicErrorCollector : ErrorCollector {31 private val failures = mutableListOf<Throwable>()32 private var mode = ErrorCollectionMode.Hard33 private val clues = mutableListOf<Any>()34 override fun getCollectionMode(): ErrorCollectionMode = mode35 override fun setCollectionMode(mode: ErrorCollectionMode) {36 this.mode = mode37 }38 override fun pushClue(clue: Any) {39 clues.add(0, clue)40 }41 override fun popClue() {42 clues.removeAt(0)43 }44 override fun clueContext(): List<Any> = clues.toList()45 override fun pushError(t: Throwable) {46 failures.add(t)47 }48 override fun errors(): List<Throwable> = failures.toList()49 override fun clear() = failures.clear()50}51fun clueContextAsString() = errorCollector.clueContext().let {52 if (it.isEmpty()) "" else it.joinToString("\n", postfix = "\n")53}54/**55 * If we are in "soft assertion mode" will add this throwable to the56 * list of throwables for the current execution. Otherwise will57 * throw immediately.58 */...
clues.kt
Source:clues.kt
1package io.kotest.assertions2/**3 * Add [clue] as additional info to the assertion error message in case an assertion fails.4 * Can be nested, the error message will contain all available clues.5 *6 * @param thunk the code with assertions to be executed7 * @return the return value of the supplied [thunk]8 */9inline fun <R> withClue(clue: Any, thunk: () -> R): R {10 return clue.asClue { thunk() }11}12/**13 * Similar to `withClue`, but will add `this` as a clue to the assertion error message in case an assertion fails.14 * Can be nested, the error message will contain all available clues.15 *16 * @param block the code with assertions to be executed17 * @return the return value of the supplied [block]18 */19inline fun <T : Any, R> T.asClue(block: (T) -> R): R {20 try {21 errorCollector.pushClue(this)22 return block(this)23 } finally {24 errorCollector.popClue()25 }26}27inline fun <T : Any> Iterable<T>.forEachAsClue(action: (T) -> Unit) = forEach { element ->28 element.asClue {...
clues
Using AI Code Generation
1import io.kotest.assertions.clues2class MyTest : StringSpec({3"test" {4clues(5a.shouldBe(2),6b.shouldBe(3),7c.shouldBe(4),8d.shouldBe(5)9}10})11import io.kotest.assertions.fail12class MyTest : StringSpec({13"test" {14fail("failed")15}16})17import io.kotest.assertions.shouldBe18class MyTest : StringSpec({19"test" {20a.shouldBe(2)21b.shouldBe(3)22c.shouldBe(4)23d.shouldBe(5)24}25})26import io.kotest.assertions.shouldContain27class MyTest : StringSpec({28"test" {29val a = listOf(1, 2, 3, 4)30a.shouldContain(5)31}32})33import io.kotest.assertions.shouldContainAll34class MyTest : StringSpec({35"test" {36val a = listOf(1, 2, 3, 4)37a.shouldContainAll(2, 3, 4)38}39})40import io.kotest.assertions.shouldContainExactly41class MyTest : StringSpec({42"test" {43val a = listOf(1, 2, 3, 4)44a.shouldContainExactly(1, 2, 3, 4)45}46})47import io.kotest.assertions.shouldContainExactlyInAnyOrder48class MyTest : StringSpec({49"test" {50val a = listOf(1, 2, 3, 4)
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!!