Skip to content

Instantly share code, notes, and snippets.

@daneko
Last active December 10, 2015 19:28
Show Gist options
  • Save daneko/4481496 to your computer and use it in GitHub Desktop.
Save daneko/4481496 to your computer and use it in GitHub Desktop.
specs2 関連メモ 便利だと思ったり、忘れそうなことをメモしておく予定 scalacheckとか使ったりした時のmemo sbt関連も書いとく

ScalaCheckとともに

  • 範囲指定有りとか
  • 本当はsetにrngを与えればどうにかなりそうなんだけど…
  • "org.scalacheck" %% "scalacheck" % "1.10.0"
import org.specs2.{ ScalaCheck, Specification }
import org.scalacheck._

// format: OFF
class SampleSpecs extends Specification with ScalaCheck { def is=

  "1 to 10 random"           ! e1().ex1 ^ endp^
  "Range normal random"       ! e2().ex2 ^ end

  // format: ON

  case class e1() {
    implicit def a = Arbitrary { for { a <- Gen.oneOf(1 to 10) } yield a }

    def ex1 = prop { (a: Int, b: Int) =>
      println(a + "/" + b)
      (a + b) must_== (b + a)
    }.set(minTestsOk -> 10)
  }

  case class e2() {
    def ex2 = prop { (a: Int, b: Int) =>
      println(a + "/" + b)
      (a + b) must_== (b + a)
    }.set(minTestsOk -> 10)
  }
}

Specs で DBを扱う時の注意点

  • DBのsetup/cleanup等を行なう時

    • Specsは基本並列で動作
    • 且つSpecファイル単位でも並列
    • 従って注意が必要
    • 参考
  • なんか本当はもっとうまくいく気がする

import org.specs2._

/**
 * Dbを使用するUnitTestを実行するためのTestSuiteみたいな
 * 各Specificationを順次実行するようにsequentialを明記
 */
class UseDbSpec extends Specification {def is =

    args(sequential = true) ^
    new HogeSpecification   ^
    new FugaSpecification ...
}

/**
 * 個々で実行するやつも sequential にする
 */ 
class HogeSpecification extends Specification with … {def is =

    args(sequential = true) ^
    ...
}
  • sbtで実行時は「*Spec」クラス以外も対象になるので設定を書く
// どのクラスを実行対象とするかを明記
testOptions := Seq(Tests.Filter(s => Seq("Spec", "Unit").exists(s.endsWith(_))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment