テストコード用DSL
● Matchersをミックスインすることで利用できる多彩な等価演算子/Assert
○ Array(1,2) == Array(1, 2)はfalse
○ Array(1, 2) should equal(Array(1, 2))はtrue
○ “hoogge” should startWith regex (“h(o*)”)
○
○ val a = 1; a should be < 2; a should be >= 0
○
○ val file = new File(“./hoge.txt”); file should be a ‘file
○ val file = new File(“./dir”); file should be a ‘directory
○ ...
タグの付け方 - FlatSpec
"Thisbrowser" must "is able to hold 100 or more tabs" taggedAs(Slow) in {
(1 to 100).foreach.browser.openNewTab
}
it must "download patches over HTTP correctly" taggedAs(Slow, Network) in {
val file = browser.download(“http://…”)
file.md5sum should equal “.....”
}
loan fixtureのコード例1
object TypicalData{ def establish: DataType = ??? }
class Example extends FlatSpec {
def withTypicalData(testScenario: DataType => Any): Unit = {
val data = TypicalData.establish
testScenario(data)
}
}
16.
loan fixtureのコード例2
“The data”should “contain lines, all of which start with ‘hoge’” in withTypicalData {
data =>
data.lines.foreach(_ should startWith regex “hoge.*”)
}
withFixtureのコード例1
trait Example1 extendsFlatSpec {
override def withFixture(test: NoArgTest) = {
setupForExample1
super.withFixture(test) match { // superで親のwithFixtureを呼ぶ=スタック呼出!
case failed: Failed => failed
case other => other
}
19.
withFixtureのコード例2
trait Example2 extendsFlatSpec {
override def withFixture(test: NoArgTest) = {
setupForExample2
super.withFixture(test) match {
case failed: Failed => failed
case other => other
}
20.
withFixtureのコード例3(完結)
class Example extendsExample1 with Example2 with {
override def withFixture(test: NoArgTest) = {
super.withFixture(test) match {...}
}
“All stacked fixtures” should “be called!” in { … }
}
21.
withFixture + loan-fixture(OneArgTest)
●『withFixtureをスタックさせつつ、さらに定型的なテストデータをテストケースにぶち
込みたいゾ』という欲求に応える
def withFixture(test: OneArgTest) = {
val fixture = ...
withFixture(test.toNoArgTest(fixture))
}
“This test” should “succeeds” in { fixture => … }