Specs2を試す(5)
Pending until fixed
一時的にテストを実行したくないときは pendingUntilFixed が使える。
// テスト1 import org.specs2._ class HelloWorldSpec2 extends Specification { def is = "This is a specification to check the 'Hello world' string" ^ p^ "The 'Hello world' string should" ^ "contain 11 characters" ! e1.pendingUntilFixed^ "start with 'Hello'" ! e2^ "end with 'world'" ! e3^ end def e1 = "Hello world" must have size(10) def e2 = "Hello world" must startWith("Hello") def e3 = "Hello world" must endWith("world") }
レポートにも 'pending' と出ます。
[info] The 'Hello world' string should [info] * contain 11 characters Pending until fixed [info] + start with 'Hello' [info] + end with 'world' [info] [info] Total for specification HelloWorldSpec2 [info] Finished in 196 ms [info] 3 examples, 0 failure (-1), 0 error, 1 pending (+1)
Auto-Examples
テストの description をそのままコードに置換えることができます。
DSLとかAPIの使いかたを示したいときに便利。
// テスト2 import org.specs2._ class AutoExampleSpec extends Specification { def is = "beNone checks if an element is None" ^ { None must beNone } ^ { Some(1) must not be none } ^end }
レポートにはテストコードがそのまま出ます。
[info] beNone checks if an element is None [info] + None must beNone [info] + Some(1) must not be none [info]
この機能を使う上で覚えておくべきこと
- ソースファイルが src/test/scala 以下にあること。なおこのディレクトリは specs2.srcTestDir というプロパティで上書き可能。
- ソースコードの展開は簡単なものしかできません。動的にやろうとしても多分コケる。
- ブロックが複数行でも表示される。
- うしろにさらに Fragment がつながっていればそれも表示される。
- endはつけましょう。
- パッケージとディレクトリ構造は合わせる。例えば package が com.myproj.homuhomu ならソースファイルは com/myproj/homuhomu に置く。
- descFromExpectations を使う手もある。ちょっと出力変わるけど。レポートを出力するときのフラグの一つをいじっているっぽい。
// テスト3: descFromExpectations の例 import org.specs2._ class AutoExampleSpec extends Specification { def is = "beNone checks if an element is None" ^ descFromExpectations ^ { None must beNone } ^ { Some(1) must not be none } ^end }
[info] beNone checks if an element is None [info] + 'None' is None [info] + 'Some(1)' is not None [info]
Using the Example description
description は body の部分で使うことができます。
// テスト4 import org.specs2._ class UsingExampleSpec extends Specification { def is = "Using the Example description" ^ "おひょひょひょひょひょ" ! {(s: String) => println(s); success }^ end }
> test [info] Compiling 1 Scala source to /home/toshi/work/scala-specs2-example/target/scala-2.9.1.final/test-classes... おひょひょひょひょひょ [info] Using the Example description [info] + おひょひょひょひょひょ [info]