Testing with SBT
Regardless of which version of SBT you want to use, basically you need to do the following:
- Include the required test framework as a test dependency in your project configuration.
- Create a dedicated test folder in the source tree, usually src / test / scala, if it is already missing.
- As always: write your tests, specifications ...
These basic steps are identical for the sbt 0.7 branch (this is one of the google code) and the current sbt 0.10 branch (now it is developed and documented on github). However, there are slight differences in defining test dependencies, since 0.10 provides a new quick setup method that is not available in 0.7.
Dependency Definition for SBT 0.7
This is how you create a basic test (based on scalacheck) with sbt 0.7. Create a new sbt 0.7 project by calling sbt in an empty folder. Go to the folder of the automatically created project and create a new build folder
# cd [your-project-root]/project
change to the newly created build folder and create your first Project.scala project file with the following contents:
class Project(info: ProjectInfo) extends DefaultProject(info) { val scalacheck = "org.scala-tools.testing" %% "scalacheck" % "1.9" % "test" }
Since for 0.7 a test folder is created automatically, you can immediately start your first test. Step to the βCreate a simple testβ paragraph.
Dependency Definition for SBT 0.10
For 0.10, you can use the sbt console to add dependencies. Just run sbt in the project directory and enter the following commands:
set libraryDependencies + = "org.scala-tools.testing" %% "scalacheck"% "1.9"% "test" save session
Then you can close the sbt console and see the build.sbt project files. Since you can easily notice that the above libraryDependencies line has been added to your quick configuration of your projects.
Since 0.10 does not automatically create source folders. You need to create a test folder yourself:
# cd [project-root]
What you need to get started with 0.10. Moreover, the testing documentation from 0.10 is much more detailed than the old one. More information can be found on the wiki page.
Create a simple scalacheck test
Create the following test file src / test / scala / StringSpecification.scala (taken from the main page of the downloads):
import org.scalacheck._ object StringSpecification extends Properties("String") { property("startsWith") = Prop.forAll((a: String, b: String) => (a+b).startsWith(a)) property("endsWith") = Prop.forAll((a: String, b: String) => (a+b).endsWith(b)) // Is this really always true? property("concat") = Prop.forAll((a: String, b: String) => (a+b).length > a.length && (a+b).length > b.length ) property("substring") = Prop.forAll((a: String, b: String) => (a+b).substring(a.length) == b ) property("substring") = Prop.forAll((a: String, b: String, c: String) => (a+b+c).substring(a.length, a.length+b.length) == b ) }
As already stated, this basic test will fail for the "concat" specification, but these are the main testing steps necessary to start testing and sbt. Just adapt the included dependency if you want to use a different testing framework.
Run tests
To run the test, open the sbt console and type
> test
This will run all the tests present in your src / test tree, whether these tests are Java or scala. This way, you can easily reuse existing Java unit tests and convert them step by step to scala.