LoginSignup
0
0

More than 5 years have passed since last update.

ScalaTestを使ってみる1

Posted at

今日はScalaTestをREPLで使ってみます。

jarの読み込み

ScalaTestのjarファイルをREPLに読み込みます。:require命令を使います。Scalaのバージョンによっては命令がなかったり、異なる命令だったりするので注意してください。

c:\work>scala
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_7
5).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :require scalatest_2.11-2.2.4.jar
Added 'c:\work\scalatest_2.11-2.2.4.jar' to classpath.

試験クラスの作成

試験クラスを作成します。色々な作り方があるようですが、取り敢えずはFlatSpecを拡張し、Matchersを加えたクラスを作れば良いです。

その前に、必要なものを輸入しておきましょう。

scala> import org.scalatest.FlatSpec
import org.scalatest.FlatSpec

scala> import org.scalatest.Matchers
import org.scalatest.Matchers

取り敢えずExampleSpecという名称のクラスを作成し、「3は2であるべきである」という試験項目と「3は3であるべきである」という試験項目を追加しました。

scala> class ExampleSpec() extends FlatSpec with Matchers {
     | "integer 3" should "equal to 2" in {
     | 3 should equal (2)
     | }
     | it should "equal to 3" in {
     | 3 should equal (3)
     | }
     | }
defined class ExampleSpec

試験の実行

試験を実行するにはrunを使えば良いです。

その前に、必要なものを輸入しておきましょう。

scala> import org.scalatest.run
import org.scalatest.run

runの第1引数として前項で作成したExampleSpecクラスの実体を渡します。

scala> run(new ExampleSpec())
[32mExampleSpec:[0m
[32minteger 3[0m
[31m- should equal to 2 *** FAILED ***[0m
[31m  3 did not equal 2 (<console>:15)[0m
[32m- should equal to 3[0m

文字化けしていますが、試験の実行自体は行われています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0