Skip to content

Instantly share code, notes, and snippets.

@yukihane
Last active January 12, 2022 00:30
Show Gist options
  • Save yukihane/62f3a708300b5a2798c3563e83bf8eac to your computer and use it in GitHub Desktop.
Save yukihane/62f3a708300b5a2798c3563e83bf8eac to your computer and use it in GitHub Desktop.
Kotest の data driven testing, forAll と withData の違い
import io.kotest.core.spec.style.FunSpec
import io.kotest.data.forAll
import io.kotest.data.row
import io.kotest.datatest.withData
class SampleTest : FunSpec({
beforeAny { println("before any") }
beforeContainer { println("before container") }
beforeEach { println("before each") }
beforeSpec { println("before spec") }
beforeTest { println("before test") }
afterAny { println("after any") }
afterContainer { println("after container") }
afterEach { println("after each") }
afterSpec { println("after spec") }
afterTest { println("after test") }
finalizeSpec { println("finalize spec") }
// https://kotest.io/docs/framework/datatesting/data_driven_testing_4.2.0
context("forAll で行うテスト") {
forAll(row(1), row(2), row(3)) { i: Int -> println("testing forAll $i") }
}
// https://kotest.io/docs/framework/datatesting/data-driven-testing.html
context("withData で行うテスト") {
withData(1, 2, 3) { i -> println("testing withData $i") }
}
// https://kotest.io/docs/framework/datatesting/data_driven_testing_4.2.0 の最下部
context("withData を使わずテストを分ける") {
listOf(row("test1", 1), row("test2", 2), row("test3", 3)).map { (name, num) ->
test(name) {
println("$name $num")
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment