sbt+ScalaTestと戯れる(その2:ScalaTestでテスト)

sbt+ScalaTestと戯れる(その1:sbtではろーわーるど) - SHI-Zone の続きということで、sbtでScalaTestとキャッキャウフフする流れなど。

sbtにScalaTestを組み込む

project/build/ProjectName.scalaをつくります。

import sbt._
class ProjectName(info: ProjectInfo) extends DefaultProject(info) {
  val scalaTest = "org.scalatest" % "scalatest" % "1.2"
}

"1.2"の部分がScalaTestのバージョン指定です。
今回はScalaが2.8.1なので1.2です。
2.7系の場合は1.1でよかったはず。

プロジェクトを更新してScalaTestをインストールする

> reload
[info] Recompiling project definition...
[info] 	  Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Building project test 1.0 against Scala 2.8.1
[info]    using test with sbt 0.7.4 and Scala 2.7.7
> update
[info] 
[info] == update ==
[info] :: retrieving :: test#test_2.8.1 [sync]
[info] 	confs: [compile, runtime, test, provided, system, optional, sources, javadoc]
[info] 	1 artifacts copied, 0 already retrieved (1742kB/138ms)
[info] == update ==
[success] Successful.
[info] 
[info] Total time: 1 s, completed 2011/01/19 1:24:38
> 

あっという間にScalaTestさんがやってきました。

折角なので俺はこのScalaTestでTDDの真似事をするぜ

さっくりとテストコードなどを作ってみます。
特に意味のない、二つの文字列を結合して返すjoinメソッドを持ったStringJoinerオブジェクトのテストコードを書きます。
src/test/scala/StringJoinerTest.scala

import org.scalatest.FunSuite

class StringJoinerTest extends FunSuite {
  test("String Join") {
    assert(StringJoiner.join("a", "b") === "ab")
  }
}

テストします

> test
[info] 
[info] == compile ==
[info]   Source analysis: 0 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Nothing to compile.
[info]   Post-analysis: 2 classes.
[info] == compile ==
[info] 
[info] == copy-resources ==
[info] == copy-resources ==
[info] 
[info] == copy-test-resources ==
[info] == copy-test-resources ==
[info] 
[info] == test-compile ==
[info]   Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling test sources...
[error] /home/razon/test/src/test/scala/StringJoinerTest.scala:5: not found: value StringJoiner
[error]     assert(StringJoiner.join("a", "b") === "ab")
[error]            ^
[error] one error found
[info] == test-compile ==
[error] Error running test-compile: Compilation failed
[info] 
[info] Total time: 3 s, completed 2011/01/19 1:30:58
> 

ねえよバカくたばれとか怒られちゃいました。当然ですね。
そんな訳でStringJoinerをさっくりと実装しちゃいます。
/src/main/scala/StringJoiner.scala

object StringJoiner { 
  def join(str1: String, str2: String) = str1 + str2
}

適当ですね。
再度テストにチャレンジしてみます。

> test
[info] 
[info] == compile ==
[info]   Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Compilation successful.
[info]   Post-analysis: 4 classes.
[info] == compile ==
[info] 
[info] == copy-resources ==
[info] == copy-resources ==
[info] 
[info] == test-compile ==
[info]   Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling test sources...
[info] Compilation successful.
[info]   Post-analysis: 2 classes.
[info] == test-compile ==
[info] 
[info] == copy-test-resources ==
[info] == copy-test-resources ==
[info] 
[info] == test-start ==
[info] == test-start ==
[info] 
[info] == StringJoinerTest ==
[info] Test Starting: String Join
[info] Test Passed: String Join
[info] == StringJoinerTest ==
[info] 
[info] == test-complete ==
[info] == test-complete ==
[info] 
[info] == test-finish ==
[info] Passed: : Total 1, Failed 0, Errors 0, Passed 1, Skipped 0
[info]  
[info] All tests PASSED.
[info] == test-finish ==
[info] 
[info] == test-cleanup ==
[info] == test-cleanup ==
[info] 
[info] == test ==
[info] == test ==
[success] Successful.
[info] 
[info] Total time: 7 s, completed 2011/01/19 1:33:49
> 

無事パスできました。
本来ならここからリファクタリングだぜってとこですが、やりようがないので今日はここまでとします。

最後に

取り敢えず触り部分をざっくり通してみましたが、当然sbtにせよScalaTestにせよ豊富な機能がもりもりあったりします。
ドキュメントを斜め読みしつつ色々とチャレンジしてみましょう。