Archive for September, 2011

Scala 2.9.1, sbt 0.10 and ScalaTest step-by-step

Some while ago, I wrote a post about getting Scala 2.8.0.RC1, sbt and ScalaTest to work together. Well, things have moved on somewhat since then, so I thought it was time to update it.

  1. Install sbt 0.10.1
  2. Create the root directory for your project:
    $ mkdir aproject
  3. Create a settings file in this directory called build.sbt containing:
    name := "A Project"
    
    version := "0.1"
    
    scalaVersion := "2.9.1"
    
    libraryDependencies ++= Seq(
      "org.scalatest" %% "scalatest" % "1.6.1" % "test"
    )
  4. Create src/main/scala/Widget.scala containing:
    package com.example
    
    class Widget {
      def colour = "Blue"
      def disposition = "Awesome"
    }
  5. Create src/test/scala/WidgetTest.scala containing:
    package com.example.test
    
    import org.scalatest.FunSuite
    import com.example.Widget
    
    class WidgetTest extends FunSuite {
    
      test("colour") {
        expect("Blue") { new Widget().colour }
      }
    
      test("disposition") {
        expect("Awesome") { new Widget().disposition }
      }
    }
  6. Run your tests with sbt test. You should see:
    [info] WidgetTest:
    [info] - colour
    [info] - disposition
    [info] Passed: : Total 2, Failed 0, Errors 0, Passed 2, Skipped 0
  7. Create src/main/scala/Main.scala containing:
    package com.example
    
    object Main {
      def main(args: Array[String]) {
        val w = new Widget()
        println("My new widget is "+ w.colour)
      }
    }
  8. Run your program with sbt run. You should see:
    [info] Running com.example.Main
    My new widget is Blue