SPECE

Posted 2010年12月12日 by

specs

残るは、SPECSです。

このテストの特徴は、BDDと呼ばれる仕様を書くような感覚で検証コードを書く設計思想です。

SPECSのホームページは、http://code.google.com/p/specs/です。

SPECSを使うためには、ここのダウンロードページの「http://code.google.com/p/specs/downloads/listから specs_2.8.1-1.6.6.jar をダウンロードして、EclipsのプロジェクトにこのJARファイルを登録します。

BDD: Behavior Driven Development でコードを書く例として、



“テスト仕様の説明文” in { 検証コード }


4つのテストケースを定義してみましょう。

参考にした例を見ながら、仕様を書いてみると、

1. ”‘hello has 13 characters”

2. ”‘hello matches ‘h.* s.*'”

3. ”‘hello now”

4. ”‘helloSSS now”


package test
import org.specs._
//import org.specs.matcher._
//import org.specs.runner.{ JUnitSuiteRunner, JUnit }
import java.util.Date
import seedo.hello

class helloSpecTest extends SpecificationWithJUnit {
	val h = new hello
	"'hello has 13 characters" in {
		h.greeting.size must_== 13
	}
	"'hello matches 'h.* s.*'" in {
		h.greeting must be matching("H.* S.*")
	}
	"'hello now" in {
		h.now must_== new Date
	}
	"'helloSSS now" in {
		h.nowSSS must_== h.nowSSS
	}
}
object specstestHello {
	def main(args: Array[String]) {
		new helloSpecTest().main(args)
	}
}



実行画面がこんな感じです。



Specification “helloSpecTest”
x ‘hello has 13 characters
’12’ is not equal to ’13’ (specstestHello.scala:13)
+ ‘hello matches ‘h.* s.*’
+ ‘hello now
x ‘helloSSS now
‘20101212 203144357’ is not equal to ‘20101212 203144343’ (specstestHello.scala:22)
Total for specification “helloSpecTest”:
Finished in 0 second, 227 ms
4 examples, 4 expectations, 2 failures, 0 error




テストケース1は、”Hello! Scala”は、12文字なので、13文字が合格とテストケースを定義していますから不合格となっています。

テストケース2は、greeting関数が返す文字列のパターンマッチング。HがきてSとなる文字列なので合格。

テストケース3は、hellowクラスのnow関数は、合格ということは、オブジェクトで比較してるのか、toStringしたもので比較したのかわかりません。

テストケース4は、nowSSS関数は、わざと同じ関数を呼び出して比較させてみたところ、14m秒の遅延があります。一致していないということで不合格。


もう少し、細かい機能を見ていきましょう。

should という機能があり、テストを章立てすることができます。

テストケースは完成しているけど、まだコード実装が完了していないのでテストしないとう場合には、skip という機能が使えます。
acceptとtagを使ってテスト項目の制御もできます。

“テスト仕様の章立て” should { 検証コード }

package test
import org.specs._
//import org.specs.matcher._
//import org.specs.runner.{ JUnitSuiteRunner, JUnit }
import java.util.Date
import seedo.hello

class helloSpecTest extends SpecificationWithJUnit {
	val h = new hello
	"テスト仕様 その1" should {
		accept("このテストは厳密に")
		"'hello has 13 characters" in {
			h.greeting.size must_== 13
		} tag("このテストは厳密に")
		"'hello matches 'h.* s.*'" in {
			h.greeting must be matching("H.* S.*")
		} tag("このテストは厳密に")
		"'hello now" in {
			h.now must_== new Date
		} tag("このテストは厳密に")
		"'hello nowSSS now" in {
			h.nowSSS must_== h.nowSSS
		}
	} tag("基本的な機能")
	"テスト仕様 その2" should {
		skip("コードがまだ完成していないのでテストを飛ばす")
		"'hellow append" in {
			h.append("ABC") must_== "XYZ"
		}
	}
}
object specstestHello {
	def main(args: Array[String]) {
		new helloSpecTest().main(args)
	}
}



実行結果は以下のようになります。

hello nowSSS にはtag を付けなかったので実行されていません。
skipを記述した「テスト仕様 その2」はテストされていません。


Specification “helloSpecTest”
テスト仕様 その1 should
x ‘hello has 13 characters
’12’ is not equal to ’13’ (specstestHello.scala:13)
+ ‘hello matches ‘h.* s.*’
+ ‘hello now
o ‘hello nowSSS now
not tagged for execution (ExampleLifeCycle.scala:178)

Total for SUS “テスト仕様 その1”:
Finished in 0 second, 96 ms
4 examples (1 skipped), 3 expectations, 1 failure, 0 error

Total for specification “helloSpecTest”:
Finished in 0 second, 170 ms
4 examples (1 skipped), 3 expectations, 1 failure, 0 error


Eclips画面はこんな感じです。

コメントを残す