Kotest Soft Assertions

Soft assertions use assertSoftly to group and execute multiple assertions.

Soft Assertions

Soft assertions let several assertions run together and report all failures at once. They are useful when you want to see every failed condition instead of stopping at the first failure.

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

This is similar to JUnit’s assertAll.

You can also pass a subject to assertSoftly. Inside the block, assertions are applied to that subject.

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

Example:

import io.kotest.assertions.assertSoftly
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class SoftAssertionTest : FunSpec({
    test("soft assertions") {
        val person = Person("devkuma", 30)

        assertSoftly(person) {
            name shouldBe "devkuma"
            age shouldBe 30
        }
    }
})

data class Person(val name: String, val age: Int)

Soft assertions can also be configured through project configuration so that they are applied implicitly to all tests.


References