SBT and Specs for BDD in Scala

· Read in about 2 min · (250 words) ·

I had used RSpec earlier for Behaviour Development Development for a Ruby on Rails project. Today I learnt how to do BDD in Scala.

Chapter 4 of Programming Scala introduces Traits and Specs for testing the code. Here is how I set it up:

I already had Scala installed. So the first step was to setup sbt ( Simple Build Tool for Scala ). Setup was easy ( described in detail here ):

$ sudo yum localinstall http://scalasbt.artifactoryonline.com/scalasbt/sbt-native-packages/org/scala-sbt/sbt//0.12.2/sbt.rpm

Then, I created a sample project which is hosted on github ( https://github.com/tuxdna/trait-with-sbt-spec ):

$ git clone git://github.com/tuxdna/trait-with-sbt-spec.git
$ cd trait-with-sbt-spec
$ sbt test

And here is the output

...
[info] Compiling 9 Scala sources to ...
[info] Compiling 4 Scala sources to ...
[info] ButtonClickableObserverSpec
[info] A Button Observer should
[info]   + observer button clicks
[info] ButtonObserverSpec
[info] A Button Observer should
[info]   + observe button clicks
[info] ButtonClickableObserverVetoableSpec
[info] A Button Observer with Vetoable Clicks should
[info]   + observe only the first button click
[info] ButtonObserverAnonSpec
[info] A Button Observer should
[info]   + observe button clicks
[info] Passed: : Total 4, Failed 0, Errors 0, Passed 4, Skipped 0
[success] Total time: 19 s, completed Feb 21, 2013 9:04:57 PM

Setting up SBT was easy, only that it took some time to fetch the libraries from maven repos. Its configuration is very similar to Maven POM files. And then adding specification was only a matter of extending ‘Specification’ class.

References: