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

7 Responses to “Scala 2.9.1, sbt 0.10 and ScalaTest step-by-step”


  1. 1 tantrum September 6, 2011 at 3:52 pm

    Thanks for this useful little guide.

  2. 2 smpman September 9, 2011 at 9:24 am

    Thanks! Simple indeed.

  3. 3 ac September 24, 2011 at 9:33 pm

    thanks. saved me some time.

  4. 4 csg February 24, 2012 at 2:43 pm

    Hi!
    Thanks for your post.
    I followed your steps but im running again and again with this annoying issue

    [error] /home/xxx/scala-test/src/test/scala/WidgetTest.scala:3: object scalatest is not a member of package org
    [error] import org.scalatest.FunSuite

    No problems typing import org.scalatest.FunSuite in test:console. so…

    Must be something with de classpath and dependencies managed, but too newbie with scala.

    Any help?. Thanks a lot
    Running sbt 0.11.2 and tried 0.10.1.

  5. 5 paul February 24, 2012 at 5:40 pm

    I can’t immediately think what would cause that behaviour. But if you stick your code in a GitHub repo and point me at it, I’ll take a look and see if I can spot what’s up…

  6. 6 JM May 8, 2012 at 12:03 am

    I had the same problem as poster csg, object scalatest is not a member of package org, did you manage to resolve it for the poster above? Thanks.

  7. 7 JM May 8, 2012 at 12:18 am

    Belay that last question, it seems to have gone away, that was weird. I moved the scala file outside of my project built it and then moved it back in and the problem went away. Probably some kind of weird library dependency error. Thanks anyway.


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





Follow

Get every new post delivered to your Inbox.

Join 243 other followers

%d bloggers like this: