Kotlintest 中的数据 Table 测试 - 高级方法名称和测试用例的传播
Data Table tests in Kotlintest - advanced method names and spreading of test cases
我正在使用 Kotlintest 和数据表来测试使用 Kotlin、SpringBoot 和 Gradle 的应用程序,因为当您的表中有复杂数据时,语法比 ParameterizedJunitTests 更简洁。
有没有办法像 parameterized tests in JUnit? Moreover, all my test-executions are listed as one test, but I would like to have a row in my test results per data table row. I found neither of the two topics in the Documentation 那样使用 method-titles 中的参数名称。
为了更清楚地说明 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在使用数据表的时候有没有类似@ParameterizedTest(name = "adding {0} and {1} should result in {2}")
的东西?
这可以使用 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
您可以反向嵌套。不要在 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
}
}
}
}
}
我正在使用 Kotlintest 和数据表来测试使用 Kotlin、SpringBoot 和 Gradle 的应用程序,因为当您的表中有复杂数据时,语法比 ParameterizedJunitTests 更简洁。
有没有办法像 parameterized tests in JUnit? Moreover, all my test-executions are listed as one test, but I would like to have a row in my test results per data table row. I found neither of the two topics in the Documentation 那样使用 method-titles 中的参数名称。
为了更清楚地说明 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在使用数据表的时候有没有类似@ParameterizedTest(name = "adding {0} and {1} should result in {2}")
的东西?
这可以使用 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
您可以反向嵌套。不要在 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
}
}
}
}
}