あいつの日誌β

働きながら旅しています。

Simple Easy Scala Testing With Play framework

Simple Easy Scala Testing With Play framework

install

scala and sbt

% brew install scala sbt

play

% which play                                                                                    
~/Development/play-2.2.2/play

create sample app

% play new myapp
What is the application name? [myapp]
> myapp

Which template do you want to use for this new application?

  1             - Create a simple Scala application
  2             - Create a simple Java application

> 1
OK, application myapp is created.

Have fun!

change directory

cd myapp

create test

create HelloWorldSepc.scala.

% cat test/HelloWorldSepc.scala
import org.specs2.mutable._

class HelloWorldSpec extends Specification {

  "The 'Hello world' string" should {
    "contain 11 characters" in {
      "Hello World" must have size(11)
    }   

    "start with 'Hello'" in {
      "Hello World" must startWith("Hello")
    }   
    
    "end with 'world'" in {
      "Hello world" must endWith("world")
    }   
  }
}

do testing.

% sbt compile "test-only HelloWorldSpec" 
...
[success] Total time: 1 s, completed 2014/04/09 12:58:41
[info] HelloWorldSpec
[info] The 'Hello world' string should
[info] + contain 11 characters
[info] + start with 'Hello'
[info] + end with 'world'
[info] Total for specification HelloWorldSpec
[info] Finished in 33 ms
[info] 3 examples, 0 failure, 0 error
[info] Passed: Total 3, Failed 0, Errors 0, Passed 3
[success] Total time: 2 s, completed 2014/04/09 12:58:43

enjoy:)