kotest

Multiple assertions (assertAll) - Kotest


Is any way to check multiple assertions in Kotest in DSL style - without Assertions.assertAll method from JUnit?

Can I write something like

firstValue shouldBe 1
and secondValue shouldBe 2

Instead of

assertAll(
    { fistValue shouldBe 1 },
    { secondValue shouldBe 2 })

Solution

  • I usually do it with assertSoftly. It probably is exactly what you want. From the documentation

    assertSoftly {
      foo shouldBe bar
      foo should contain(baz)
    }
    

    Or using it as a parameter

    assertSoftly(foo) {
        shouldNotEndWith("b")
        length shouldBe 3
    }
    

    However, your syntax works just as fine. You don't really need to assert softly.

    firstValue shouldBe 1
    secondValue shouldBe 2
    

    will execute both assertions. If the first one fails the test crashes early. With assertSoftly, both assertions will be checked.