IntelliJ Kotest 不显示测试因异常而失败

IntelliJ Kotest doesn't show tests failed with exception

代码

我有以下三个测试:

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 个测试 运行,其中 1 个失败,但是 IntelliJ 和 Kotest 插件向我显示的是 2 个测试中有 2 个通过了。我可以在“测试结果”侧面板中看到有些地方不对,但它没有任何有用的信息。

如果我导航到带有测试结果的 index.html,我可以正确地看到所有内容。我想在 IntelliJ 中看到相同的数据。

截图

IntelliJ 输出: 注意:

index.html 测试结果:

其他信息

如果在 GivenWhen 块内抛出异常,则测试初始化​​失败。如果我 运行 只有一个测试,这里是输出:

似乎异常只在 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 级冗余缩进。

上述代码的替代方法是使用不同的 kotest testing style,例如 ShouldSpec.

此问题已在 5.1.0 中解决。