【问题标题】:IntelliJ Kotest doesn't show tests failed with exceptionIntelliJ Kotest 不显示测试失败并出现异常
【发布时间】:2021-12-30 11:59:50
【问题描述】:

代码

我有以下三个测试:

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 输出: 请注意:

  • 左侧缺少数字 1 的测试
  • 在顶部显示“测试通过:2 个,共 2 个”
  • 左侧有黄色叉号直到“Given”,但“Given”中的所有测试都是绿色的

index.html 附测试结果:

其他信息

  • Kotest 版本:4.6.3
  • IntelliJ Kotest 插件版本:1.1.49-IC-2021.2.3

【问题讨论】:

  • kotest 的哪个版本
  • kotest 版本 4.6.3

标签: kotlin intellij-idea kotest


【解决方案1】:

此问题已在 5.1.0 中解决。

【讨论】:

    【解决方案2】:

    如果在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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多