Getting started with sbt, scala and scalatest

2010-04-23

sbt is a simple build tool for Scala projects and seems to become the standard build tool for Scala projects.

scalatest is a sophisticated testing frameworks that claims to increase your productivity by letting you write fewer lines of test code. It supports different testing styles like Unit Testing or Behaviour Driven Testing.

Unfortunately scala 2.8.x has no binary compatibility to its former version 2.7.x what makes it a bit tricky to get started. But it‘s worth trying.

Here is a simple starter for using sbt and scalatest with scala 2.8.0:

Download sbt

cd ~/bin
wget http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.4.jar

Create your sbt launcher script

vi sbt
===
#!/bin/sh

java <del>jar $(dirname $0)/sbt</del>launch-0.7.4.jar "$@"
===

Make it executable for the current user:

chmod u+x sbt

Create an empty folder or change to an existing Scala project on your local disk and run sbt

cd my-fancy-scala-project
sbt

Type y to create a new project configuration and fill in your project name, organization and project version.

For the Scala version type: 2.8.0

sbt downloads the required Scala libraries and afterwards you should see the sbt prompt waiting for your commands. For now we type exit.

Create the sbt project configuration file.

mkdir project/build
vi project/build/MyProject.scala
import sbt._

class MyProject(info: ProjectInfo) extends DefaultProject(info) {

// Dependencies
val scalatest = "org.scalatest" % "scalatest" % "1.2"

}

Update your project (download dependencies)

In the project configuration we declared the dependency to the scalatest library as a so-called managed dependency. To download those dependencies type the following command:

sbt update

As already said this command downloads all managed dependencies if not already cached on your local machine. Now you are ready to use scalatest.

Create a simple Test Specification

vi src/test/scala/StackSpec.scala
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers

import java.util.Stack

class StackSpec extends FlatSpec with ShouldMatchers {

"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should equal (2)
stack.pop() should equal (1)
}

it should "throw EmptyStackException if an empty stack is popped" in {
val emptyStack = new Stack[String]
evaluating { emptyStack.pop() } should produce [java.util.EmptyStackException]
}
}

Run your test with sbt

sbt test

That‘s all.
One of the exciting features worth to mention are triggered executions. You can ask sbt to run tests automatically when the source code changed. sbt ~test-quick for example, fires all failed tests and all tests that has been effected by the changes.

I would like to see a sbt plugin for intellij or netbeans that integrates the (triggered) test results in the IDE‘s test view with its stacktrace navigation.


me

Marco Rico Gomez is a passionate software developer located in Germany who likes to share his thoughts and experiences about software development and technologies with others.


blog comments powered by Disqus