Manipulating Test Execution with ScalaTest

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:

  1. class ExampleTest extends FunSuite {
  3.     override def withFixture(test : NoArgTest){
  4.         println
  5.         println("before")
  6.         super.withFixture(test)
  7.         println("after")
  8.     }
  10.     test("example test"){
  11.         println("succeding test")
  12.     }
  14.     test("example failing test"){
  15.         println("failing test")
  16.         fail("embrace failure")
  17.     }
  18. }

This produces the following output on the console:

succeding test

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:

  1. trait ExampleRule extends AbstractSuite {
  2.     self : Suite =>
  3.     override abstract def withFixture(test : NoArgTest) {
  4.         println
  5.         println("before")
  6.         try {
  7.             ExampleRule.super.withFixture(test)
  8.         } finally {
  9.             println("after")
  10.         }
  11.     }
  12. }
  14. class Example2Test extends FunSuite with ExampleRule {
  15.     test("example test") {
  16.         println("succeding test")
  17.     }
  19.     test("example failing test") {
  20.         println("failing test")
  21.         fail("embrace failure")
  22.     }
  23. }

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:

  1. trait Example3Rule extends AbstractSuite {
  2.     self : Suite =>
  3.     override abstract def withFixture(test : NoArgTest) {
  4.         println("3 before")
  5.         try {
  6.             Example3Rule.super.withFixture(test)
  7.         } finally {
  8.             println("3 after")
  9.         }
  10.     }
  11. }
  14. @RunWith(classOf[JUnitRunner])
  15. class Example3Test extends FunSuite  with Example3Rule with ExampleRule{
  16.     test("example test") {
  17.         println("succeding test")
  18.     }
  20.     test("example failing test") {
  21.         println("failing test")
  22.         fail("embrace failure")
  23.     }
  24. }
3 before
succeding test
3 after

3 before
failing test
3 after

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

  • DZone
  • Digg
  • Reddit
  • Facebook
  • Twitter