Reputation: 5962
If I have some variables that I need set up for EACH TEST IN A SUITE, is it possible to somehow set them up and get them into the test without writing a suite for each test?
I.e., setup:
val actorRef = context.actorOf(Props(new MyTestDude))
Teardown:
actorRef ! PoisonPill
If I run with setup and teardown hooks I need to have the actorref as a field in the test class so it's accessible from the setup method but then I can't run the tests parallel as everyone will share the same state.
Upvotes: 2
Views: 1631
Reputation: 15557
For your information you can do something similar in specs2 by using the AroundOutside
implicit context:
import org.specs2._
import execute._
import specification._
class s extends mutable.Specification {
// this context will be used for each example
implicit def withActor = new AroundOutside[ActorRef] {
// actor is passed to each example
val actor = context.actorOf(Props(new MyTestDude))
def outside = actor
// execute the example code
def around[R : AsResult](r: =>R) = {
try AsResult(r)
finally (actor ! PoisonPill)
}
}
"My actor rules!" in { actor: ActorRef =>
actor ! "very important message"
ok
}
}
Upvotes: 3
Reputation: 84
In Scalatest, all of the Suite subclasses have some way of handling this, which is described in detail in the Scaladoc for the particular suite class.
Here is an example using FunSuite, based on the documentation. http://www.artima.com/docs-scalatest-2.0.M5b/#org.scalatest.FunSuite
class ExampleSuite extends fixture.FunSuite {
case class F(actorRef: ActorRef)
type FixtureParam = F
def withFixture(test: OneArgTest) {
val actorRef = context.actorOf(Props(new MyTestDude))
val fixture = F(actorRef)
try {
withFixture(test.toNoArgTest(fixture)) // "loan" the fixture to the test
}
finally {
actorRef ! PoisonPill
}
}
test("My actor rules!") { f =>
f.actorRef ! "very important message"
assert( ... )
}
}
Also note that Akka has a special module for ScalaTest called TestKit that will make testing actors in general much easier. http://doc.akka.io/docs/akka/snapshot/scala/testing.html
Upvotes: 2