The Scope must be created exactly where you create your example:
class FooSpec extends BaseSpec {
"foo" should {
"run specs" in {
"should fail" in new BeforeAfterScope {
true must beFalse
}
}
}
}
[UPDATE: 10-12-2015 for specs2 3.x]
Note that if you don't need to inherit values from the BeforeAfterScope
trait, it is actually simpler to have the BaseSpec
extend org.specs2.specification.BeforeAfterEach
and define the before
and after
method there.
If, on the other hand, you want to do some setup/teardown before and after all your examples you need the BeforeAfterAll
trait. Here is a specification using both traits:
import org.specs2.specification.{BeforeAfterAll, BeforeAfterEach}
trait BaseSpec extends Specification with BeforeAfterAll with BeforeAfterEach {
def beforeAll = println("before all examples")
def afterAll = println("after all examples")
def before = println("before each example")
def after = println("after each example")
}
class FooSpec extends BaseSpec {
"foo" should {
"run specs" in {
"should fail" in {
true must beFalse
}
}
}
}
This approach is documented here.