ひ孫

犬のこととか書いていきたい

scala specs2のbeforeではまった話。

scalaのspecで事前処理がしたくなったのでBeforeExampleを使おうと下記のようなテストを書いてみた。

class SampleExampleSpec extends Specification with BeforeExample{
  def before = {
    println("bef")
    println("for")
  }

  "foo" should {
    "baa" in {
      println("baa")
      1 must_== 1
    }
    "baz" in {
      println("baz")
      1 must_== 1
    }
  }
}

予想では bef -> for -> baa -> bef -> for -> baz となるだろうなと思っていたが結果は

bef
for
bef
for
baa
baz

beforeが先に二回実行されている・・・・?

散々悩んだが下記の様な記事にぶち当たった http://stackoverflow.com/questions/8026866/parallel-execution-of-tests

なるほど。scala specsは並列に実行されるのがデフォルトになっていると。 解除するには

sequential

をつければいいらしい。

うーん、だいぶはまってしまった。