My favorite feature in the JUnit are Rules. With Rules you can manipulate the execution of your test, by wrapping your own code around the test execution. I have used this for setting up databases for test, making sure they execute in the EDT, limiting legal test execution time and for set up and tear down of various other resources.

So naturally when working with ScalaTest I pretty soon wanted to have something equivalent to Rules. While you find not much when searching for 'Rules ScalaTest', it is actually quite easy to implement.

No matter what kind of tests you write with ScalaTest, at the end of the day the method def withFixture(aTest : NoArgTest) will get called with your test to execute. 'Your test' means in case of a FunSuite test the stuff between the curly braces that you pass to the test method.

The implementation of withFixture will finally execute your test. So all you have to do in order to get the equivalent of a JUnit Rule is to override withFixture. Lets give it a try:

class ExampleTest extends FunSuite {

override def withFixture(test : NoArgTest){
println
println("before")
super.withFixture(test)
println("after")
}

test("example test"){
println("succeding test")
}

test("example failing test"){
println("failing test")
fail("embrace failure")
}
}

This produces the following output on the console:

before
succeding test
after

before
failing test

As you can see, if you want to do stuff after the test, you better put it in a finally block, so it gets executed no matter if the test fails or not.

While this works, we don't want to explicitly override withFixture every time we need our 'rule'. The solution to this is to extract it into a trait:

trait ExampleRule extends AbstractSuite {
self : Suite =>
override abstract def withFixture(test : NoArgTest) {
println
println("before")
try {
ExampleRule.super.withFixture(test)
} finally {
println("after")
}
}
}

class Example2Test extends FunSuite with ExampleRule {
test("example test") {
println("succeding test")
}

test("example failing test") {
println("failing test")
fail("embrace failure")
}
}

The trait extends AbstractSuite, which makes withFixture available. It has a selftype of Suite, in order to ensure it gets only mixed into test suites.

So how does this solution compare to the implementation of a JUnit Rule? With a Junit Rule we have to create the actual Rule plus a Statement instance. With ScalaTest we only extend a single trait. Also since all this is standard Scala inheritance an no reflection involved, so it is pretty strait forward to find out how this works. While how and why a Rule gets executed is kind of tricky from the source alone. Finally you can explicitly control the order in which your traits apply, by controlling the order in which you put them in the extends clause:

trait Example3Rule extends AbstractSuite {
self : Suite =>
override abstract def withFixture(test : NoArgTest) {
println("3 before")
try {
Example3Rule.super.withFixture(test)
} finally {
println("3 after")
}
}
}

@RunWith(classOf[JUnitRunner])
class Example3Test extends FunSuite with Example3Rule with ExampleRule{
test("example test") {
println("succeding test")
}

test("example failing test") {
println("failing test")
fail("embrace failure")
}
}

before
3 before
succeding test
3 after
after

before
3 before
failing test
3 after
after

With Junit Rules there is currently no ready to use way to do this, but a feature to achieve this is in development.

Talks

Wan't to meet me in person to tell me how stupid I am? You can find me at the following events: