KZKY memo

自分用メモ.

Scala Test

基本

Eclipse + Scala開発で,Testをするのにどうしたらよいのか.
簡単にまとめた.

Scala Test Installation

Scala IDE for Eclipse

この手順は,多分いらない.

ScalaTest Jar

  • build.gradle
dependencies {
	testCompile group: 'org.scalatest', name: 'scalatest_2.11', version: '2.2.2'
}

ScalaTestコード

Eclispe上

非常にハマったが,@RunWith(classOf[JUnitRunner])を付けないと
No Junit tests foundがでる.

package edu.kzk.scalatest

import org.scalatest.FlatSpec
import org.scalatest.Matchers
import scala.collection.mutable.Stack
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class ExampleSpec extends FlatSpec with Matchers {

    "A Stack" should "pop values in last-in-first-out order" in {
        val stack = new Stack[Int]
                stack.push(1)
                stack.push(2)
                stack.pop() should be (2)
                stack.pop() should be (1)
    }

    it should "throw NoSuchElementException if an empty stack is popped" in {
        val emptyStack = new Stack[Int]
                a [NoSuchElementException] should be thrownBy {
            emptyStack.pop()
        } 
    }
}

"" shoud ..., "" must ..., it shout ..., it must ..

のようなBDD-styleでテスト可能.

コマンド

$ scala -cp scalatest_2.11-2.2.2.jar org.scalatest.run ExampleSpec