我有几个案例的简单说明:
class MySpec extends Specification {
"Something" should {
"case 1" in {
...
}
"case 2" in {
...
}
}
}
现在我需要启动应用程序,运行所有案例,然后关闭应用程序。启动/停止应用程序非常耗时,我不希望它在每种情况下都发生。
如何在案例开始之前和所有案例结束之后运行代码?
最佳答案
我根据 cmbaxter 的回答提出了以下解决方案。
import org.specs2.specification.Step
trait BeforeAllAfterAll extends Specification {
// see http://bit.ly/11I9kFM (specs2 User Guide)
override def map(fragments: =>Fragments) =
Step(beforeAll) ^ fragments ^ Step(afterAll)
protected def beforeAll()
protected def afterAll()
}
然后在Specification
中混合BeforeAllAfterAll
并实现beforeAll
和afterAll
方法:
class MySpec extends Specification with BeforeAllAfterAll {
def beforeAll() {
println("Doing setup work...")
}
def afterAll() {
println("Doing shutdown work...")
}
"Something" should {
"case 1" in {
...
}
"case 2" in {
...
}
}
}
最后,提取初始化以在规范之间共享:
trait InApplication extends BeforeAllAfterAll {
def beforeAll() {
println("Doing setup work...")
}
def afterAll() {
println("Doing shutdown work...")
}
}
class MySpec extends Specification with InApplication {
"Something" should {
"case 1" in {
...
}
"case 2" in {
...
}
}
}
关于scala - 在规范之前和之后执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16936811/