Specs2を試す(1)

sbtのバージョンは0.10
Scalaのバージョンは2.9.1

インストール

githubから持ってきてpublish-local(localのivyリポジトリにpublish)することにした。

 $ git clone git://github.com/etorreborre/specs2
 $ sbt
 > publish-local

sbtの設定

http://etorreborre.github.com/specs2/#Dependencies に従い、sbtの設定ファイルにDependencyを加える。

libraryDependencies ++= Seq(
    "org.specs2" %% "specs2" % "1.6.1",
    "org.specs2" %% "specs2-scalaz-core" % "6.0.1" % "test"
  )

resolvers ++= Seq("snapshots" at "http://scala-tools.org/repo-snapshots",
                  "releases"  at "http://scala-tools.org/repo-releases")

libraryDependenciesは依存ライブラリの設定。resolversは使用するmavenリポジトリの設定。

Specification

参考: http://etorreborre.github.com/specs2/guide/org.specs2.guide.QuickStart.html#Quick+Start

Specs2はUnit specification/Acceptance specificationの2通りの書き方がある

Unit specification

Unit specification はorg.specs2.mutable.Specificationを継承するやりかた。

  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")
      }
    }
  }
Acceptance specification

Acceptance specification org.specs2.Specification はorg.specs2.Specificationを継承するやりかた。

  import org.specs2._

  class HelloWorldSpec extends Specification { def is =

    "This is a specification to check the 'Hello world' string"                 ^
                                                                                p^
    "The 'Hello world' string should"                                           ^
      "contain 11 characters"                                                   ! e1^
      "start with 'Hello'"                                                      ! e2^
      "end with 'world'"                                                        ! e3^
                                                                                end
    def e1 = "Hello world" must have size(11)
    def e2 = "Hello world" must startWith("Hello")
    def e3 = "Hello world" must endWith("world")
  }

"description" ! body という書き方をして一つ一つのspecを記述し、
specを^でつないでそのリストを作る
pは空行を入れたいときに使う。


ところでAcceptanceのネーミングがしっくりこないんだけどだれか説明してくれないかなあ