Reputation: 69
My scenario for test is that I have following hierarchy:
AfterAll
AfterAll
Test Suite for Component 1 with multiple test cases
BeforeAll
AfterAll
Test Suite for Component 2 with multiple test cases
BeforeAll
AfterAll
Test Suite for Component 2 with multiple test cases
BeforeAll
BeforeAll
Now I have the idea that I can run my setup part before and after a Suite and each Test Case, but is there a way that i can run my setup before and after all Test Suites
Upvotes: 1
Views: 2793
Reputation: 28511
You can leverage inheritance to replicate the setup across a variety of suites, it's slightly manual but it's a very common approach.
trait DefaultSuite extends Suite with BeforeAndAfterAll with Informing {
override def beforeAll(): Unit = {..}
override def afterAll(): Unit = {..}
}
class Component1Tests extends FlatSpec with DefaultSuite {}
class Component2Tests extends FlatSpec with DefaultSuite {}
class Component3Tests extends FlatSpec with DefaultSuite {}
If you want something that only runs once, before and after everying, you need to get a little smarter. In some cases an SBT plugin or task to deal with the most advanced scenarios, in others you do something like this:
object Singleton {
val dbConnection = DB(..)
}
trait DefaultSuite extends Suite with BeforeAndAfterAll with Informing {
def dbConnection: DB = Singleton.dbConnection
}
So implementors of your DefaultSuite
will be able to access a bunch of things easily, but behind the scenes there is only a single instance of a particular object. I've used this technique quite successfully in the past, where a singleton and a trait are used to provide "fake" inheritance, but actually you are referencing the same instance of various objects.
Upvotes: 3