NOTE!! This page has yet to be updated for the 0.7 release.

Example Test

This page describes writing an example test for sbt. The example is the webapp/jsp test from sbt trunk.

Define the Build

Create a project that will contain your tests. The project should use the latest version of sbt for testing. Declare sbt's test framework as a plugin:


import sbt._
class Plugins(info: ProjectInfo) extends PluginDefinition(info)
	val sbtTest = "org.scala-tools.sbt" % "test" % "0.5.6"

and use it in the project definition:

import sbt._
class Tests(info: ProjectInfo) extends DefaultProject(info) with test.ScalaScripted
	val ivy = "org.apache.ivy" % "ivy" % "2.0.0"

Ivy is required as a dependency because of an implementation detail in the test framework.

Define the Test

Each test is an sbt project that goes in src/sbt-test/<group-name>/<test-name>. For example, src/sbt-test/web/jsp. Create project/build.properties containing a project.name and project.version:


project.name=JSP Test

A test is defined by creating a script called 'test' that defines a series of commands to run and whether they should fail or succeed. For example:

> update

> jetty-run

> check-page

> jetty-stop

The '>' signifies that what follows is an action to run on the project. A '$' would signify a command to run. Examples of commands are 'delete', 'copy-file', and 'reload'. The [success] means that the action or command should succeed. [failure] would mean the command should fail. A full description of test scripts is at http://code.google.com/p/simple-build-tool/wiki/ChangeDetectionAndTesting#Testing.

The check-page action is defined in the build for the test project:


import sbt._
class JSP(info: ProjectInfo) extends DefaultWebProject(info)
	val j6 = "org.mortbay.jetty" % "jetty" % "6.1.17" % "test"
	val j = "org.mortbay.jetty" % "jsp-2.0" % "6.1.17" % "test"

	def indexURL = new java.net.URL("http://localhost:8080")
	def indexFile = new java.io.File("index.html")
	import Process._
	lazy val getPage = execTask { indexURL #> indexFile }
	lazy val checkPage = task { checkHelloWorld() } dependsOn getPage

	private def checkHelloWorld() =
			FileUtilities.readString(indexFile, log) match
				case Right(value) =>
					if(value.contains("Hello World!")) None
					else Some("index.html did not contain 'Hello World!' :\n" +value)
				case Left(msg) => Some(msg)
		finally { jettyInstance.stop() }

The final file in the test is src/main/webapp/index.jsp:

		<% out.println(" Hello World!"); %>

Run the Test

Run update first to get Ivy as a dependency.

The scripted action runs all scripted tests in src/sbt-test. The scripted-only action accepts the tests to run in the form <group>/<name>. For example:

> update
> scripted
> scripted-only web/jsp

You can download the example test project and use it as a starting point for your tests.