代码
我有以下三项测试:
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
class Example {
fun blah(number: Int): Int {
if (number == 1) {
throw IllegalArgumentException()
} else {
return number
}
}
}
class ExampleTest : BehaviorSpec({
Given("example") {
val example = Example()
When("calling blah(0)") {
val result = example.blah(0)
Then("it returns 0") {
result shouldBe 0
}
}
When("calling blah(1)") {
val result = example.blah(1)
Then("it returns 1") {
result shouldBe 1
}
}
When("calling blah(2)") {
val result = example.blah(2)
Then("it returns 2") {
result shouldBe 2
}
}
}
})问题
中间测试抛出一个意外异常。我希望看到3个测试运行,其中一个失败了,但是IntelliJ和Kotest插件告诉我的是,每2个测试中就有2个通过了测试。我可以在测试结果侧面板中看到一些不正确的东西,但是它没有任何有用的信息。
如果我导航到具有测试结果的index.html,我可以正确地看到一切。我希望在IntelliJ中看到相同的数据。
截图
IntelliJ输出:

请注意:
具有测试结果的index.html :

其他信息

发布于 2022-02-22 16:31:23
这个问题在5.1.0中得到了解决。
发布于 2022-01-06 10:34:38
如果在Given或When块中引发异常,则测试初始化失败。如果只运行这一测试,下面是输出:

似乎只有在Then块中才能处理异常。
这意味着可以抛出异常的所有东西都应该进入Then块,这又意味着设置和操作不能在测试之间共享:
class ExampleTest : BehaviorSpec({
Given("example") {
When("calling blah(0)") {
Then("it returns 0") {
val example = Example()
val result = example.blah(0)
result shouldBe 0
}
}
}
Given("example") {
When("calling blah(1)") {
Then("it returns 1") {
val example = Example()
val result = example.blah(1)
result shouldBe 1
}
}
}
Given("example") {
When("calling blah(2)") {
Then("it returns 2") {
val example = Example()
val result = example.blah(2)
result shouldBe 2
}
}
}
})这也会导致2级冗余缩进。
上述代码的另一种选择是使用不同的考特测试风格,例如ShouldSpec。
https://stackoverflow.com/questions/70531355
复制相似问题