我使用Kotlintest和data tables来测试一个使用Kotlin、SpringBoot和Gradle的应用程序,因为当你的表中有复杂的数据时,它的语法要比ParameterizedJunitTests简洁得多。
有没有办法在方法标题中使用参数名,就像parameterized tests in JUnit一样?此外,我的所有测试执行都作为一个测试列出,但我希望在我的测试结果中每个数据表行都有一行。我在Documentation中找不到这两个主题。
为了让事情变得更清楚,用Kotlintest举个例子:
class AdditionSpec : FunSpec() {
init {
test("x + y is sum") {
table(
headers("x", "y", "sum"),
row(1, 1, 2),
row(50, 50, 100),
row(3, 1, 2)
).forAll { x, y, sum ->
assertThat(x + y).isEqualTo(sum)
}
}
}
}并给出了相应的JUnit示例:
@RunWith(Parameterized::class)
class AdditionTest {
@ParameterizedTest(name = "adding {0} and {1} should result in {2}")
@CsvSource("1,1,2", "50, 50, 100", "3, 1, 5")
fun testAdd(x: Int, y: Int, sum: Int) {
assertThat(x + y).isEqualTo(sum);
}
}1/3失败: Kotlintest:

Junit:

在使用数据表时,kotlintest中有没有类似于@ParameterizedTest(name = "adding {0} and {1} should result in {2}")的东西?
发布于 2019-12-09 00:48:44
可以像这样使用FreeSpec和减号运算符:
class AdditionSpec : FreeSpec({
"x + y is sum" - {
listOf(
row(1, 1, 2),
row(50, 50, 100),
row(3, 1, 2)
).map { (x: Int, y: Int, sum: Int) ->
"$x + $y should result in $sum" {
(x + y) shouldBe sum
}
}
}
})这在Intellij中提供了以下输出:

请参阅此here上的文档
发布于 2019-12-09 15:20:49
您可以反转嵌套。不用在test中使用table,只需将test嵌套在table中即可。
class AdditionSpec : FunSpec() {
init {
context("x + y is sum") {
table(
headers("x", "y", "sum"),
row(1, 1, 2),
row(50, 50, 100),
row(3, 1, 2)
).forAll { x, y, sum ->
test("$x + $y should be $sum") {
x + y shouldBe sum
}
}
}
}
}https://stackoverflow.com/questions/58303081
复制相似问题