给出的是kotlin-test github docs中的示例,但是我在这里没有看到beforeEach或beforeClass概念。我想了解
DateTimeFormatter.ofPattern("dd MMM yyyy'T'HH:mm")
.format(LocalDateTime.now()).toUpperCase();
答案 0 :(得分:1)
与您自己的答案@JTeam非常相似,但是使用init {}构造函数块声明您的测试,然后您可以直接在类中重写方法。
class MyTest : StringSpec() {
override fun beforeTest(description: Description) {
super.beforeTest(description)
println("Before every spec/test case")
}
override fun beforeSpec(description: Description, spec: Spec) {
super.beforeSpec(description, this)
println("Before every test suite")
}
override fun afterTest(description: Description, result: TestResult) {
super.afterTest(description, result)
println("After every spec/test case")
}
override fun afterSpec(description: Description, spec: Spec) {
super.afterSpec(description, spec)
println("After every test suite")
}
init {
"test should run " {
"Hello".shouldHaveLength(4)
}
"test2 should run " {
"Hello World".shouldHaveLength(10)
}
}
}
答案 1 :(得分:0)
在github docs和kotlin-test框架源代码中进行了一些研究之后,下面是编写beforeTest,beforeSpec,afterTest,afterSpec的代码
class MyTest : StringSpec({
"test should run " {
"Hello".shouldHaveLength(4)
}
"test2 should run " {
"Hello World".shouldHaveLength(10)
}
}) {
override fun beforeTest(description: Description) {
super.beforeTest(description)
println("Before every spec/test case")
}
override fun beforeSpec(description: Description, spec: Spec) {
super.beforeSpec(description, this)
println("Before every test suite")
}
override fun afterTest(description: Description, result: TestResult) {
super.afterTest(description, result)
println("After every spec/test case")
}
override fun afterSpec(description: Description, spec: Spec) {
super.afterSpec(description, spec)
println("After every test suite")
}
}
这看起来并不优雅,如果有任何方法可以使其优雅,请发布它。