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:
-
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.






Hi Jens,
http://blog.schauderhaft.de doesn’t link to your current article, but on “Input Validation with Scala and Swing”
I wondered, if you were on vacation for the last 2 month …
Hi Karl, thats bad.
Especially since I can’t reproduce it. Something seems to be caching the site but I don’t know what, nor how to fix it.
I’ve had the same issues some weeks ago also wondering if the post-on-sunday behavior was given up or something else happened
… you may consider a
but the http header sent has a expiration in the past – so I thought that everything should be fine. So don’t know why I or anyone else did not see the latest post…
narf… i considered using a pragma no cache meta tag – which was of course successfully erased from the comment
@Karl can you email me th complete html of start page that you get?
The old version that some people see comes from before a switch of servers I had to do at that time. If that gives anybody an idea what is going on, It would be awesome
I’ll try it tomorrow at work.
Our proxy now returns the correct page, seems that loading one of the newer pages invalidated the old one :/
@Karl I killed a couple of plugins and now it seems to work.
Thx to everybody helping with the caching problem.