Scala 2.8.0.RC1, sbt and ScalaTest step-by-step

For an updated version of this post that covers Scala 2.9.1 and sbt 0.10, go here.

I spent quite a bit of time beating my head against the details of getting Scala 2.8.0.RC1, sbt and ScalaTest to play nicely with each other. So here’s a step-by-step guide which will hopefully save others time.

  1. Install sbt
  2. Create a new project as follows:
    $ mkdir aproject
    $ cd aproject/
    $ sbt
    Project does not exist, create new project? (y/N/s) y
    Name: aproject
    Organization: com.example
    Version [1.0]:
    Scala version [2.7.7]: 2.8.0.RC1
    sbt version [0.7.3]:
    
  3. Create a build configuration file in project/build/AProject.scala containing:
    import sbt._
    
    class AProject(info: ProjectInfo) extends DefaultProject(info) {
      val scalaToolsSnapshots = ScalaToolsSnapshots
      val scalatest = "org.scalatest" % "scalatest" %
        "1.0.1-for-scala-2.8.0.RC1-SNAPSHOT"
    }
    

    This tells sbt that you will be using the pre-release snapshot version of ScalaTest for Scala 2.8.0.RC1.

  4. Download the ScalaTest library with sbt update. After running this, you should see that you have a lib_managed directory containing the ScalaTest jar.
  5. Create src/main/scala/Widget.scala containing:
    package com.example
    
    class Widget {
      def colour = "Blue"
      def disposition = "Awesome"
    }
  6. Create src/test/scala/WidgetSpec.scala containing:
    package com.example.test
    
    import org.scalatest.Spec
    import com.example.Widget
    
    class WidgetSpec extends Spec {
    
      describe("A Widget") {
    
        it("should be blue") {
          expect("Blue") { new Widget().colour }
        }
    
        it("should be awesome") {
          expect("Awesome") { new Widget().disposition }
        }
      }
    }
    
  7. Run your tests with sbt test. You should see:
    [info] == com.example.test.WidgetSpec ==
    [info] A Widget
    [info] Test Starting: A Widget should be blue
    [info] Test Passed: A Widget should be blue
    [info] Test Starting: A Widget should be awesome
    [info] Test Passed: A Widget should be awesome
    [info] == com.example.test.WidgetSpec ==
    
  8. 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)
      }
    }
    
  9. Run your program with sbt run. You should see:
    [info] Running com.example.Main
    My new widget is Blue
    

Update: Use the pre-defined ScalaToolsSnapshots repo (thanks to Erick Fleming).

Update: Fixed a typo in step 8 (thanks to Jason).

6 Responses to “Scala 2.8.0.RC1, sbt and ScalaTest step-by-step”


  1. 1 Ben April 24, 2010 at 2:23 pm

    Useful post, already top Google hit for “2.8.0.RC1 scalatest sbt”. Cheers!

  2. 2 Jason April 25, 2010 at 8:45 pm

    Great resource, thanks for posting. There’s an omission in that with Step 8, ‘Create src/main/Main.scala containing:’ it should instead be ‘Create src/main/scala/Main.scala containing:’

  3. 3 Joe May 12, 2010 at 3:44 pm

    For those interested for RC2 use
    “1.2-for-scala-2.8.0.RC2-SNAPSHOT”

  4. 4 Steve November 24, 2010 at 9:06 pm

    Great tutorial! Thanks for sharing!

  5. 5 Gonzalo June 3, 2011 at 8:13 pm

    For scala 2.9.0 I used
    val scalatest = “org.scalatest” % “scalatest_2.9.0” % “1.4.1”


  1. 1 Toni Cebrián » Simple Scala project step-by-step Trackback on August 24, 2011 at 2:26 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: