开发者

Basic testing functionality in SBT

How do I create a simple unit test for my application using SBT's test feature?

I'm hoping the answer is that I can write a single file in src/test/scala for my project that imports some special testing package from SBT which makes writing tests as easy as writing a single method.

The tutorial ExampleSbtTest seems to be doing something more complicated than what I n开发者_如何学编程eed, and I can't find anything simpler on the SBT GoogleCode page.


Testing with SBT

No matter which version of SBT you want to use, basically you have to do the following steps:

  1. Include your desired testing framework as test-dependency in your project configuration.
  2. Create a dedicated testing folder within your source tree, usually src/test/scala, if it isn't present already.
  3. As always: Write your tests, specs ...

Those basic steps are identical for the sbt 0.7 branch (that's the one from google-code) and the current sbt 0.10 branch (now developed and documented on github). However, there are minor differences how to define the testing dependencies since 0.10 provides a new quick configuration method not present in 0.7.

Defining the dependency for SBT 0.7

Here 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. Change into the automatically created project folder and create a new build folder

# cd [your-project-root]/project
# mkdir build

change into the newly created build folder and create your first project build file Project.scala with the follwing content:

class Project(info: ProjectInfo) extends DefaultProject(info) {

    val scalacheck = "org.scala-tools.testing" %% "scalacheck" % "1.9" % "test"

}

Since for 0.7 the testing folder is created automatically you can then start to write your first test right away. Step to the paragraph "Create a simple test".

Defining the dependency for SBT 0.10

For 0.10 one can use the sbt console to add the dependency. Just start sbt in your project directory and enter the following commands:

set libraryDependencies += "org.scala-tools.testing" %% "scalacheck" % "1.9" % "test" session save

You can then close the sbt console and have a look at your projects build.sbt file. As you can easily spot the above libraryDependencies line was added to your projects quick configuration.

Since 0.10 doesn't create the source folders automatically. You have to create the testing folder on your own:

# cd [project-root]
# mkdir -p src/test/scala

That's all it takes to get started with 0.10. Moreover, the documentation about testing with 0.10 is far more detailed then the old one. See the testing wiki page for further details.

Create a simple scalacheck test

Create the following test file src/test/scala/StringSpecification.scala (taken from the scalacheck homepage):


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 indicated this basic check will fail for the "concat" specification, but that are the basic testing steps needed to get started with testing and sbt. Just adapt the included dependency if you want to use another testing framework.

Run your tests

To run your test, open the sbt console and type

> test

That will run all tests present in your src/test tree no matter if those are java or scala based tests. So you can easily reuse your existing java unit tests and convert them step by step to scala.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜