How to use data driven in Kotest with style AnnotationSpec()
Example for JUnit 5:
@ParameterizedTest
@ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) // six numbers
void isOdd_ShouldReturnTrueForOddNumbers(int number) {
assertTrue(Numbers.isOdd(number));
}
How can I do this in Kotest? This is what I tried to come up with:
class AnnotationSpecExample : AnnotationSpec() {
@Test
fun test1(value: Int) {
value shouldBe 1
}
}
I couldn't find documentation and examples.
The Annotation spec style is an easy way to migrate your JUnit tests to Kotest, but it doesn't have much to offer and I suggest that you use another more "Kotest-idiomatic" style instead, like FunSpec
.
The counterpart of parameterized tests in Kotest would be data driven testing in Kotest. In a FunSpec
test, your example looks like this, using withData
from the kotest-framework-datatest
module:
class OddNumberTest : FunSpec({
context("odd numbers") {
withData(1, 3, 5, -3, 15, Integer.MAX_VALUE) { number ->
number should beOdd()
}
}
})
The problem when you try to use Annotation Spec style here, is that you cannot use withData
in a leaf scope, but only in root or container scopes, and each test in an Annotation spec is a leaf.