After doing some research in the github docs and kotlin-test framework source code, below is the code to write 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")
}
}
This is not looking elegant, if there is any way which can make it elegant, please post it.