How to use shouldFail method of io.kotest.assertions.fail class

Best Kotest code snippet using io.kotest.assertions.fail.shouldFail

JsonLiteralsTest.kt

Source:JsonLiteralsTest.kt Github

copy

Full Screen

2import io.kotest.assertions.json.NumberFormat3import io.kotest.assertions.json.TypeCoercion4import io.kotest.assertions.json.compareJsonOptions5import io.kotest.assertions.json.shouldEqualJson6import io.kotest.assertions.shouldFail7import io.kotest.assertions.throwables.shouldThrow8import io.kotest.core.spec.style.FunSpec9import io.kotest.matchers.string.shouldContain10import io.kotest.matchers.throwable.shouldHaveMessage11class JsonLiteralsTest : FunSpec(12 {13 test("Unsupported type") {14 shouldThrow<IllegalArgumentException> {15 "0x03" shouldEqualJson "0x03"16 }.shouldHaveMessage("Unsupported kotlinx-serialization type 0x03")17 }18 context("Strict (default) comparisons") {19 test("comparing float and int") {20 shouldFail {21 "3.2" shouldEqualJson "3"22 }.shouldHaveMessage(23 """24 The top level expected 3 but was 3.225 expected:<3> but was:<3.2>26 """.trimIndent()27 )28 }29 test("quoted numbers are treated as strings") {30 shouldFail { "\"1E3\"" shouldEqualJson "1000.0" }31 // Unquoted 1E3 is parsed to double and back due to prettifying output32 shouldFail { "\"1000.0\"" shouldEqualJson "1E3" }.message shouldContain33 "The top level expected number but was string"34 shouldFail { "10.0" shouldEqualJson "\"10.0\"" }.message shouldContain35 "The top level expected string but was number"36 }37 test("comparing exponent-based float with regular float") {38 "1E3" shouldEqualJson "1000.0"39 "1000.0" shouldEqualJson "1E3"40 "1000.0" shouldEqualJson "1000"41 "5E0" shouldEqualJson "5.0"42 "2E-1" shouldEqualJson "0.2"43 shouldFail {44 "1.0E-3" shouldEqualJson "0.0001"45 }46 "1.0E-4" shouldEqualJson "0.0001"47 }48 test("comparing high-precision floating point numbers") {49 // Note: In the middle paragraph of the failure message the expected JSON has been50 // formatted as a JSON tree using KotlinX.serialization which parses the51 // number to a double and back, hence the loss of precision.52 shouldFail {53 "0.12345678912345678" shouldEqualJson "0.123456789123456789"54 }.shouldHaveMessage(55 """56 The top level expected 0.123456789123456789 but was 0.1234567891234567857 expected:<0.123456789123456789> but was:<0.12345678912345678>58 """.trimIndent()59 )60 }61 test("comparing string and boolean") {62 shouldFail {63 "true" shouldEqualJson "\"true\""64 }.shouldHaveMessage(65 """66 The top level expected string but was boolean67 expected:<"true"> but was:<true>68 """.trimIndent()69 )70 }71 }72 context("CompareMode.Exact requires same format for numbers") {73 infix fun String.shouldExactlyEqualJson(expected: String) =74 this.shouldEqualJson(expected, compareJsonOptions { numberFormat = NumberFormat.Strict })75 test("comparing float and exponent") {76 shouldFail {77 "10.0" shouldExactlyEqualJson "1e1"78 }.shouldHaveMessage(79 """80 The top level expected 1e1 but was 10.081 expected:<1e1> but was:<10.0>82 """.trimIndent()83 )84 }85 test("comparing int and exponent") {86 shouldFail {87 "10" shouldExactlyEqualJson "1e1"88 }.shouldHaveMessage(89 """90 The top level expected 1e1 but was 1091 expected:<1e1> but was:<10>92 """.trimIndent()93 )94 }95 test("comparing float and int") {96 shouldFail {97 "10.0" shouldExactlyEqualJson "10"98 }.shouldHaveMessage(99 """100 The top level expected 10 but was 10.0101 expected:<10> but was:<10.0>102 """.trimIndent()103 )104 }105 test("quoted numbers are treated as strings") {106 shouldFail { "\"1E3\"" shouldEqualJson "1000.0" }107 // Unquoted 1E3 is parsed to double and back due to prettifying output108 shouldFail { "\"1000.0\"" shouldEqualJson "1E3" }.message shouldContain109 "The top level expected number but was string"110 shouldFail { "10.0" shouldEqualJson "\"10.0\"" }.message shouldContain111 "The top level expected string but was number"112 }113 }114 context("Lenient type-conversions") {115 infix fun String.lenientShouldEqualJson(expected: String) =116 this.shouldEqualJson(expected, compareJsonOptions { typeCoercion = TypeCoercion.Enabled })117 test("comparing exponent-based float with regular float") {118 "1E3" lenientShouldEqualJson "\"1000.0\""119 "1000.0" lenientShouldEqualJson "\"1E3\""120 "2E-1" lenientShouldEqualJson "0.2"121 "2E-1" lenientShouldEqualJson "\"0.2\""122 "0.2" lenientShouldEqualJson "\"2e-1\""123 "5E0" lenientShouldEqualJson "5.0"124 }125 test("Strings with numbers") {126 shouldFail {127 "\"abc 123\"" lenientShouldEqualJson "123"128 }.shouldHaveMessage(129 """130 The top level expected number but was string131 expected:<123> but was:<"abc 123">132 """.trimIndent()133 )134 shouldFail {135 "123" lenientShouldEqualJson "\"abc 123\""136 }137 }138 test("booleans in strings are ok") {139 "true" lenientShouldEqualJson "\"true\""140 "\"true\"" lenientShouldEqualJson "true"141 }142 test("float and int can be mixed, if exactly same") {143 "1.0" lenientShouldEqualJson "1"144 "1" lenientShouldEqualJson "1.0"145 }146 test("Dont trim necessary zeroes") {147 shouldFail {148 "10" lenientShouldEqualJson "1.0"149 }150 shouldFail {151 "1" lenientShouldEqualJson "10.0"152 }153 }154 test("high-precision float with only trailing zeros") {155 "1" lenientShouldEqualJson "1.0000000000000000000000000"156 "1.0000000000000000000000000" lenientShouldEqualJson "1"157 }158 }159 }160)...

Full Screen

Full Screen

EqualIgnoringUnknownTest.kt

Source:EqualIgnoringUnknownTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.tests.json2import io.kotest.assertions.json.shouldEqualSpecifiedJson3import io.kotest.assertions.shouldFail4import io.kotest.core.spec.style.FunSpec5import io.kotest.matchers.throwable.shouldHaveMessage6import io.kotest.property.Arb7import io.kotest.property.arbitrary.Codepoint8import io.kotest.property.arbitrary.az9import io.kotest.property.arbitrary.string10import io.kotest.property.checkAll11class EqualIgnoringUnknownTest : FunSpec(12 {13 test("extra field in actual - passes") {14 checkAll(Arb.string(1..10, Codepoint.az())) { string ->15 val a = """ { "a" : "$string", "b": "bar" } """16 val b = """ { "a" : "$string" }"""17 a shouldEqualSpecifiedJson b18 }19 }20 test("expected field has different value - should fail") {21 val a = """ { "a" : "foo", "b" : "bar" } """22 val b = """ { "a" : "foo", "b" : "baz" } """23 a shouldEqualSpecifiedJson a24 shouldFail {25 a shouldEqualSpecifiedJson b26 }.shouldHaveMessage(27 """At 'b' expected 'baz' but was 'bar'28expected:<{29 "a": "foo",30 "b": "baz"31}> but was:<{32 "a": "foo",33 "b": "bar"34}>"""35 )36 }37 test("actual missing field") {38 val a = """ { "a" : "foo" } """39 val b = """ { "a" : "foo", "b": "bar" } """40 shouldFail {41 a shouldEqualSpecifiedJson b42 }.shouldHaveMessage(43 """The top level object was missing expected field(s) [b]44expected:<{45 "a": "foo",46 "b": "bar"47}> but was:<{48 "a": "foo"49}>"""50 )51 }52 context("Nested object") {53 test("extra field is ok") {54 checkAll(Arb.string(1..10, Codepoint.az())) { string ->55 val a = """ { "wrapper": { "a" : "$string", "b": "bar" } }"""56 val b = """ { "wrapper": { "a" : "$string" } }"""57 a shouldEqualSpecifiedJson b58 }59 }60 test("nested expected value differs") {61 val a = """ { "wrapper": { "a" : "foo", "b": "bar" } }"""62 val b = """ { "wrapper": { "a" : "foo", "b": "baz" } }"""63 a shouldEqualSpecifiedJson a64 shouldFail {65 a shouldEqualSpecifiedJson b66 }.shouldHaveMessage(67 """At 'wrapper.b' expected 'baz' but was 'bar'68expected:<{69 "wrapper": {70 "a": "foo",71 "b": "baz"72 }73}> but was:<{74 "wrapper": {75 "a": "foo",76 "b": "bar"77 }78}>"""79 )80 }81 test("actual missing field") {82 val a = """ { "wrapper": { "a" : "foo" } } """83 val b = """ { "wrapper": { "a" : "foo", "b": "bar" } } """84 shouldFail {85 a shouldEqualSpecifiedJson b86 }.shouldHaveMessage(87 """At 'wrapper' object was missing expected field(s) [b]88expected:<{89 "wrapper": {90 "a": "foo",91 "b": "bar"92 }93}> but was:<{94 "wrapper": {95 "a": "foo"96 }97}>"""98 )99 }100 }101 context("Arrays") {102 test("extra field is ok") {103 checkAll(Arb.string(1..10, Codepoint.az())) { string ->104 val a = """ { "wrapper": [{ "a" : "$string", "b": "bar" }] }"""105 val b = """ { "wrapper": [{ "a" : "$string" }] }"""106 a shouldEqualSpecifiedJson b107 }108 }109 test("nested expected value differs") {110 val a = """ { "wrapper": [{ "a" : "foo", "b": "bar" }] }"""111 val b = """ { "wrapper": [{ "a" : "foo", "b": "baz" }] }"""112 a shouldEqualSpecifiedJson a113 shouldFail {114 a shouldEqualSpecifiedJson b115 }.shouldHaveMessage(116 """At 'wrapper.[0].b' expected 'baz' but was 'bar'117expected:<{118 "wrapper": [119 {120 "a": "foo",121 "b": "baz"122 }123 ]124}> but was:<{125 "wrapper": [126 {127 "a": "foo",128 "b": "bar"129 }130 ]131}>"""132 )133 }134 test("actual missing field") {135 val a = """ { "wrapper": [ { "a" : "foo" } ] } """136 val b = """ { "wrapper": [ { "a" : "foo", "b": "bar" } ] } """137 shouldFail {138 a shouldEqualSpecifiedJson b139 }.shouldHaveMessage(140 """At 'wrapper.[0]' object was missing expected field(s) [b]141expected:<{142 "wrapper": [143 {144 "a": "foo",145 "b": "bar"146 }147 ]148}> but was:<{149 "wrapper": [150 {151 "a": "foo"...

Full Screen

Full Screen

BaseTest.kt

Source:BaseTest.kt Github

copy

Full Screen

...40 }41 @Suppress("NewApi")42 protected fun compileWithDagger(43 vararg sources: String,44 shouldFail: Boolean = false,45 block: KotlinCompilation.Result.() -> Unit = { }46 ) {47 fun String.clean() = replace("[^a-zA-Z0-9]".toRegex(), "_")48 val className = testInfo.testClass.get().simpleName49 val testName = testInfo.displayName50 .clean()51 .replace("_{2,}".toRegex(), "_")52 .removeSuffix("_")53 val workingDir = File("build/test-builds/$className/$testName")54 compileTangle(55 sources = sources,56 enableDaggerAnnotationProcessor = true,57 generateDaggerFactories = false,58 // Many constructor parameters are unused.59 allWarningsAsErrors = false,60 block = { checkExitCode(shouldFail).block() },61 workingDir = workingDir62 )63 }64 @Suppress("NewApi")65 protected fun TestScope.compile(66 vararg sources: String,67 shouldFail: Boolean = false,68 block: KotlinCompilation.Result.() -> Unit = { }69 ): KotlinCompilation.Result {70 fun String.clean() = replace("[^a-zA-Z0-9]".toRegex(), "_")71 val className = testInfo.testClass.get().simpleName72 val testName = testInfo.displayName73 .clean()74 .replace("_{2,}".toRegex(), "_")75 .removeSuffix("_")76 val compilerType = if (useAnvilFactories) "anvil" else "dagger"77 val workingDir = File("build/test-builds/$className/$compilerType/$testName")78 return compileTangle(79 sources = sources,80 enableDaggerAnnotationProcessor = !useAnvilFactories,81 generateDaggerFactories = useAnvilFactories,82 // Many constructor parameters are unused.83 allWarningsAsErrors = false,84 block = { checkExitCode(shouldFail).block() },85 workingDir = workingDir86 )87 }88 fun KotlinCompilation.Result.checkExitCode(shouldFail: Boolean) = apply {89 val expectedCode = if (shouldFail) {90 COMPILATION_ERROR91 } else {92 OK93 }94 messages.asClue {95 exitCode shouldBe expectedCode96 }97 }98 data class TestScope(val useAnvilFactories: Boolean)99}...

Full Screen

Full Screen

PrettyPrintTest.kt

Source:PrettyPrintTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.tests.json2import io.kotest.assertions.json.shouldEqualJson3import io.kotest.assertions.shouldFail4import io.kotest.core.spec.style.FunSpec5import io.kotest.matchers.string.shouldContain6class PrettyPrintTest : FunSpec(7 {8 test("print object") {9 shouldFail {10 "{}" shouldEqualJson """ { "a": { "b": "c", "x": 1 } }"""11 }.message shouldContain """12 {13 "a": {14 "b": "c",15 "x": 116 }17 }18 """.trimIndent()19 }20 test("print string with inner quotes") {21 shouldFail {22 "{}" shouldEqualJson """{ "a": "\"x\"" }"""23 }.message shouldContain """24 {25 "a": "\"x\""26 }27 """.trimIndent()28 }29 test("print array") {30 shouldFail {31 "{}" shouldEqualJson """[ { "x": 1 }, { "y": [2, 3] }, { "point": { "x": 1.0, "y": 2.0, "z": null } } ]"""32 }.message shouldContain """33 [34 {35 "x": 136 },37 {38 "y": [39 2,40 341 ]42 },43 {44 "point": {45 "x": 1.0,46 "y": 2.0,47 "z": null48 }49 }50 ]51 """.trimIndent()52 }53 test("print empty array and object") {54 shouldFail {55 "{}" shouldEqualJson """ { "a": {}, "b": [] } """56 }.message shouldContain """57 {58 "a": {},59 "b": []60 }61 """.trimIndent()62 }63 }64)...

Full Screen

Full Screen

OneTests.kt

Source:OneTests.kt Github

copy

Full Screen

...10@OptIn(ExperimentalKotest::class)11class OneTests : FunSpec({12 test("either fails if less than two assertions are executed") {13 val (_, beforeAssertions) = matcherState()14 val message = shouldFail {15 one { "a" shouldBe "a" }16 }.message17 withClue("either should maintain the assertion that was executed in the counter") {18 assertionCounter.get() shouldBe beforeAssertions + 219 }20 message shouldContainOnlyOnce "One cannot ensure a mutual exclusion with less than two assertions"21 }22 test("either fails if more than one assertion succeeds") {23 shouldFail {24 one {25 "a" shouldBe "a"26 "b" shouldBe "b"27 }28 }.message shouldContainOnlyOnce "One expected a single assertion to succeed, but more than one succeeded."29 }30 test("one fails if all assertions fail") {31 shouldFail {32 one {33 "a" shouldBe "b"34 "b" shouldBe "c"35 }36 }.message shouldContainOnlyOnce "One expected a single assertion to succeed, but none succeeded."37 }38 test("one succeeds when a single assertion succeeds and many fail") {39 one {40 (0..9).forEach { it shouldBe it + 1 }41 1 shouldBe 142 }43 }44})...

Full Screen

Full Screen

kotestExtTest.kt

Source:kotestExtTest.kt Github

copy

Full Screen

1package org.http4k.filter2import io.kotest.assertions.shouldFail3import org.http4k.core.Method.GET4import org.http4k.core.Request5import org.http4k.core.Response6import org.http4k.core.Status.Companion.OK7import org.http4k.core.then8import org.http4k.kotest.haveHeader9import org.junit.jupiter.api.Test10class ExtensionsTest {11 @Test12 fun `request matching as a filter`() {13 val app = RequestFilters.Assert(haveHeader("bob")).then { Response(OK) }14 app(Request(GET, "").header("bob", "foo"))15 shouldFail { app(Request(GET, "")) }16 }17 @Test18 fun `response matching as a filter`() {19 ResponseFilters.Assert(haveHeader("bob").invert()).then { Response(OK) }(Request(GET, ""))20 shouldFail { ResponseFilters.Assert(haveHeader("bob")).then { Response(OK) }(Request(GET, "")) }21 }22}...

Full Screen

Full Screen

ext.kt

Source:ext.kt Github

copy

Full Screen

1package org.http4k.kotest2import io.kotest.assertions.shouldFail3import io.kotest.matchers.Matcher4import io.kotest.matchers.should5import io.kotest.matchers.shouldNot6internal fun <T> assertMatchAndNonMatch(t: T, match: Matcher<T>, mismatch: Matcher<T>) {7 t should match8 shouldFail { t shouldNot match }9 t shouldNot mismatch10 shouldFail { t should mismatch }11}12internal fun <T> assertMatchAndNonMatch(t: T, match: T.() -> Unit, mismatch: T.() -> Unit) {13 match(t)14 shouldFail { mismatch(t) }15}...

Full Screen

Full Screen

BasicTest.kt

Source:BasicTest.kt Github

copy

Full Screen

1package io.kotest2import io.kotest.assertions.shouldFail3import io.kotest.matchers.shouldBe4import kotlin.test.Test5class BasicTest {6 @Test7 fun assertionsWorks() {8 shouldFail {9 1 shouldBe 210 }11 }12}...

Full Screen

Full Screen

shouldFail

Using AI Code Generation

copy

Full Screen

1class TestSpec : FunSpec({2test("shouldFail") {3shouldFail {4}5}6})7class TestSpec : FunSpec({8test("shouldFail") {9shouldFail {10}11}12})13class TestSpec : FunSpec({14test("shouldFail") {15shouldFail {16}17}18})19class TestSpec : FunSpec({20test("shouldFail") {21shouldFail {22}23}24})25class TestSpec : FunSpec({26test("shouldFail") {27shouldFail {28}29}30})31class TestSpec : FunSpec({32test("shouldFail") {33shouldFail {34}35}36})37class TestSpec : FunSpec({38test("shouldFail") {39shouldFail {40}41}42})43class TestSpec : FunSpec({44test("shouldFail") {45shouldFail {46}47}48})49class TestSpec : FunSpec({50test("shouldFail") {51shouldFail {52}53}54})55class TestSpec : FunSpec({56test("shouldFail") {57shouldFail {58}59}60})61class TestSpec : FunSpec({62test("shouldFail") {63shouldFail {64}

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 fail

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful