![](/img/trans.png)
[英]How to have hierarchical test report with Kotlintest and Gradle
[英]Data Table tests in Kotlintest - advanced method names and spreading of test cases
我正在使用 Kotlintest 和數據表來測試使用 Kotlin、SpringBoot 和 Gradle 的應用程序,因為當表中有復雜數據時,語法比 ParameterizedJunitTests 更簡潔。
有沒有辦法在方法標題中使用參數名稱,就像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);
}
}
使用數據表時,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
}
}
}
})
請參閱此處的文檔
您可以反向嵌套。 而不是在test
中包含table
,只需在table
嵌套test
。
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
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.