Kotest Inspectors
In addition to matchers, Kotest supports inspectors, extension functions for collections that verify whether an assertion holds for a particular group of elements.
Inspectors
Inspectors are extension functions for collections and arrays. They verify whether the given assertion is true for a specified number or group of elements.
For example, the following verifies that at least two values in xs are greater than zero.
xs.forAtLeast(2) {
it shouldBeGreaterThan 0
}
The following verifies that no element in xs is greater than zero.
xs.forNone {
it shouldBeGreaterThan 0
}
Kotest provides these inspectors:
| Inspector | Description |
|---|---|
forAll |
The assertion must pass for every element. |
forNone |
The assertion must fail for every element. |
forOne |
The assertion must pass for exactly one element. |
forAtMostOne |
The assertion must pass for at most one element. |
forAtLeastOne |
The assertion must pass for at least one element. |
forAtLeast(k) |
The assertion must pass for at least k elements. |
forAtMost(k) |
The assertion must pass for at most k elements. |
forAny |
Alias for forAtLeastOne. |
forSome |
The assertion must pass for at least one element but not all elements. |
forExactly(k) |
The assertion must pass for exactly k elements. |
Example:
import io.kotest.core.spec.style.FunSpec
import io.kotest.inspectors.forAll
import io.kotest.inspectors.forAtLeast
import io.kotest.inspectors.forNone
import io.kotest.matchers.ints.shouldBeGreaterThan
class NumbersTestWithInspectors : FunSpec({
test("inspectors") {
val numbers = listOf(1, 2, 3, 4, 5)
numbers.forAll {
it shouldBeGreaterThan 0
}
numbers.forAtLeast(2) {
it shouldBeGreaterThan 3
}
numbers.forNone {
it shouldBeGreaterThan 10
}
}
})