How to use T.asClue method of io.kotest.assertions.clues class

Best Kotest code snippet using io.kotest.assertions.clues.T.asClue

ClueTest.kt

Source:ClueTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.assertions2import io.kotest.assertions.asClue3import io.kotest.assertions.assertSoftly4import io.kotest.assertions.throwables.shouldThrow5import io.kotest.assertions.withClue6import io.kotest.core.spec.style.FreeSpec7import io.kotest.matchers.Matcher8import io.kotest.matchers.MatcherResult9import io.kotest.matchers.nulls.shouldBeNull10import io.kotest.matchers.shouldBe11import io.kotest.matchers.string.shouldContain12import io.kotest.matchers.string.shouldStartWith13import kotlinx.coroutines.delay14import kotlinx.coroutines.withTimeout15import java.util.*16class ClueTest : FreeSpec({17 "withClue()" - {18 fun withClueEcho(other: String) = object : Matcher<String> {19 override fun test(value: String) = MatcherResult(20 false,21 { "Should have the details of '$value' and $other" },22 {23 "Should have the details of '$value' and $other"24 })25 }26 "should prepend clue to message with a newline" {27 val ex = shouldThrow<AssertionError> {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" {102 data class MyData(val a: Int, val b: String)103 MyData(10, "clue object").asClue {104 shouldThrow<AssertionError> { it.b shouldBe "2" }.message shouldBe """MyData(a=10, b=clue object)105|expected:<"2"> but was:<"clue object">""".trimMargin()106 }107 data class HttpResponse(val status: Int, val body: String)108 val response = HttpResponse(404, "not found")109 response.asClue {110 shouldThrow<AssertionError> {it.status shouldBe 200}.message shouldBe "HttpResponse(status=404, body=not found)\nexpected:<200> but was:<404>"111 MyData(20, "nest it").asClue { inner ->112 shouldThrow<AssertionError> {it.status shouldBe 200}.message shouldBe "HttpResponse(status=404, body=not found)\nMyData(a=20, b=nest it)\nexpected:<200> but was:<404>"113 shouldThrow<AssertionError> {inner.a shouldBe 10}.message shouldBe "HttpResponse(status=404, body=not found)\nMyData(a=20, b=nest it)\nexpected:<10> but was:<20>"114 }115 //after nesting, everything looks as before116 shouldThrow<AssertionError> { it.status shouldBe 200 }.message shouldBe "HttpResponse(status=404, body=not found)\nexpected:<200> but was:<404>"117 }118 }119 "clue can be nullable" {120 val ex = shouldThrow<AssertionError> {121 null.asClue { 1 shouldBe 2 }122 }123 ex.message shouldBe "null\nexpected:<2> but was:<1>"124 }125 "clue should work for withTimeout" {126 shouldThrow<AssertionError> {127 withClue("timey timey") {128 withTimeout(2) {129 delay(1000)130 }131 }132 }.message shouldBe "timey timey\nTimed out waiting for 2 ms"133 }134 "clue should work where expected or actual is null" {135 shouldThrow<AssertionError> {136 withClue("A expected is null value") {137 "hello" shouldBe null138 }139 }.message shouldBe "A expected is null value\nExpected null but actual was \"hello\""140 shouldThrow<AssertionError> {141 withClue("A actual is null value") {142 null shouldBe "hello"143 }144 }.message shouldBe "A actual is null value\nExpected \"hello\" but actual was null"145 }146 }147})...

Full Screen

Full Screen

AssertionsTest.kt

Source:AssertionsTest.kt Github

copy

Full Screen

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}...

Full Screen

Full Screen

clues.kt

Source:clues.kt Github

copy

Full Screen

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 {29 action(it)30 }31}...

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.

Most used method in clues

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful