kotlin - How to write beforeEach and beforeClass in kotlintest
问题描述
Given is the example from kotlin-test github docs, but i don't see beforeEach or beforeClass concept here. I want to understand,
- How to execute a code/method once before every test
- How to execute a code/method once before every test class
class MyTests : StringSpec({
"length should return size of string" {
"hello".length shouldBe 5
}
"startsWith should test for a prefix" {
"world" should startWith("wor")
}
})
解决方案
Very similar to your own answer @JTeam, but use the init {} constructor block to declare your tests and then you can override methods directly in the class.
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)
}
}
}
推荐阅读
- dateadd - 如何编写返回昨天日期的 ZOQL (Zuora SQL) 函数?
- java - 设计springboot应用程序骨架的最佳方法是什么
- reactjs - React Router TypeError:无法读取未定义的属性“状态”
- python - 线程“主”org.apache.spark.SparkException 中的异常,在 spark 中本地运行
- java - 列出java中给定路径中.class文件中的所有方法及其返回类型
- java - 从 docker 运行时出现 Java SIGSEGV 错误
- asp.net-core - 在 Identity Server 4 中使用 AddSigningCredential(cert)
- android - 为什么 adb remount 失败?
- android - 使用 gson 将文档添加到 Firestore 时间戳问题
- java - 迁移后 IntelliJ 无法加载 JUnit5