Kotlin 如何在BehaviorSpec中创建可重用的步骤

Kotlin 如何在BehaviorSpec中创建可重用的步骤,kotlin,kotest,Kotlin,Kotest,我还是Kotlin和Kotest的新手,我正在努力寻找一种创建BDD风格测试的方法。我的问题是框架如何能够创建可重用的给定步骤。 例如: class KotestTest1 : BehaviorSpec({ given("State A") { // Verify the State A exists println("Verify the State A exists") `when`("

我还是Kotlin和Kotest的新手,我正在努力寻找一种创建BDD风格测试的方法。我的问题是框架如何能够创建可重用的给定步骤。 例如:

class KotestTest1 : BehaviorSpec({
    given("State A") {
        // Verify the State A exists
        println("Verify the State A exists")
        `when`("Action A") {
            // Execute Action A
            println("Execute Action A")
            then("State => A1") {
                // Verify the state is now A1
                println("Verify the state is now A1")
            }
        }
    }
})


class KotestTest2 : BehaviorSpec({
    given("State A") {
        // Verify the State A exists
        println("Verify the State A exists")
        `when`("Action B") {
            // Execute Action B
            println("Execute Action B")
            then("State => B1") {
                // Verify the state is now B1
                println("Verify the state is now B1")
            }
        }
    }
})
所以这里我有给定步骤“状态A”的代码重复。我想知道创建整个步骤的预期方式是什么。看起来给定(description:String)是我必须重复的内容,对于
println(“验证状态A是否存在”)
我只是将其提取到公共函数中


我希望我能更好地构建代码,这样我就能创建给定的步骤并在多个测试场景中使用它们。对此有何建议?

我认为最常用的方法是将两个测试合并为一个测试,并在其中嵌套测试:

class-KotestTest:BehaviorSpec({
给定(“A国”){
何时(“行动A”){
然后(“状态=>A1”){
}
}
何时(“行动B”){
然后(“状态=>B1”){
}
}
}
})