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.
- Install sbt
- 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]:
- 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.
- Download the ScalaTest library with
sbt update
. After running this, you should see that you have alib_managed
directory containing the ScalaTest jar. - Create
src/main/scala/Widget.scala
containing:package com.example class Widget { def colour = "Blue" def disposition = "Awesome" }
- 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 } } } }
- 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 ==
- 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) } }
- 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).
Useful post, already top Google hit for “2.8.0.RC1 scalatest sbt”. Cheers!
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:’
For those interested for RC2 use
“1.2-for-scala-2.8.0.RC2-SNAPSHOT”
Great tutorial! Thanks for sharing!
For scala 2.9.0 I used
val scalatest = “org.scalatest” % “scalatest_2.9.0” % “1.4.1”