■ はじめに
ここ最近、狂ったように、プログラムや調べものをしてきたが とりあえず、最低限やれることが調べたので、後はブラッシュアップさせていきたい、、、 っと思ったのだが、単体試験のことを忘れていたので、調べてみた。
目次
【0】Scalaのテストフレームワーク 【1】ScalaTest 1)テストスタイル 2)別テストフレームワークとの組み合わせ 【2】ScalaTestの導入 【3】Quick Start 1)テストコード 2)テスト実行 【4】Hello World
【0】Scalaのテストフレームワーク
* Scalaのテストフレームワークは、以下の通り [1] ScalaTest << ★これを使ってみる [2] specs2 [3] ScalaCheck [4] TestNG
【1】ScalaTest
https://www.scalatest.org/
https://www.scalatest.org/quick_start
1)テストスタイル
* さまざまなテストスタイル(xUnit/BDD(Behavior Driven Developmentなど) で書ける => 以下の公式ドキュメントを参照(こんなにあると思わなかった) => 個人的には、「FunSuite」か「FunSpec」かな、、、
https://www.scalatest.org/user_guide/selecting_a_style
[1] FunSuite
* xUnit形式のスタイル
[2] FunSpec
* RubyのRSpecライクなスタイル
[3] WordSpec
* specs2ライクなスタイル
2)別テストフレームワークとの組み合わせ
* Property-Based Testingとして「ScalaCheck」を使う * Mock には、Javaのモック「Mockito」を使う => Mockito については、以下の関連記事を参照のこと
Mockito ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2014/07/18/233904
Mockito ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2014/07/19/121409
【2】ScalaTestの導入
libraryDependencies ++= Seq( "org.scalatest" %% "scalatest" % "3.2.15" % "test"
https://www.scalatest.org/install
【3】Quick Start
https://www.scalatest.org/quick_start
で、テスト実行してみて テスト環境が整っているかをチェック。
1)テストコード
フォルダ構成
├─src │ ├─main │ │ └─scala │ └─test │ └─scala │ └─ HelloTest.scala
HelloTest.scala(The FlatSpec style)
import collection.mutable.Stack import org.scalatest._ import flatspec._ import matchers._ class HelloTest extends AnyFlatSpec with should.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() } } }
2)テスト実行
sbt test [info] HelloTest: [info] A Stack [info] - should pop values in last-in-first-out order [info] - should throw NoSuchElementException if an empty stack is popped [info] Run completed in 742 milliseconds. [info] Total number of tests run: 2 [info] Suites: completed 1, aborted 0 [info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed.
【4】Hello World
* 自分の簡単なコードでテストしてみる
1)サンプル
HelloWorld.scala
package com.example object HelloWorld { def sayHello(name: String): String = { require(name.nonEmpty) s"Hello, ${name}." } }
HelloWorldTest.scala
package com.example import org.scalatest.funspec.AnyFunSpec class HelloWorldTest extends AnyFunSpec { describe("Hello World Test") { describe("Normal") { it("Set name=Mike") { val result = HelloWorld.sayHello("Mike") assert(result == "Hello, Mike!!") } } describe("Abnormal") { it("should produce IllegalArgumentException when name is empty") { assertThrows[IllegalArgumentException] { HelloWorld.sayHello("") } } } } }
テスト実行
sbt test [info] HelloWorldTest: [info] Hello World Test [info] Normal [info] - should produce NoSuchElementException when head is invoked [info] - Set name=Mike [info] Abnormal [info] - should produce IllegalArgumentException when name is empty [info] Run completed in 740 milliseconds. [info] Total number of tests run: 2 [info] Suites: completed 2, aborted 0 [info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed.
関連記事
ScalaTest ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/03/28/003906
ScalaTest ~ with ScalaCheck ~
https://dk521123.hatenablog.com/entry/2023/03/29/000014
ScalaTest ~ with Mockito ~
https://dk521123.hatenablog.com/entry/2023/03/31/002830
Scala ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/03/10/193805
Scala ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/12/184331
Scala ~ 基本編 / 繰り返し ~
https://dk521123.hatenablog.com/entry/2023/01/24/000000
Scala ~ 基本編 / Option型 ~
https://dk521123.hatenablog.com/entry/2023/03/09/000000
Scala ~ 基本編 / メソッド ~
https://dk521123.hatenablog.com/entry/2023/03/03/000000
Scala ~ 基本編 / クラス ~
https://dk521123.hatenablog.com/entry/2023/03/14/000857
Scala ~ 基本編 / コレクション ~
https://dk521123.hatenablog.com/entry/2023/03/13/000345
Scala ~ 基本編 / 日付・日時 ~
https://dk521123.hatenablog.com/entry/2023/03/08/000000
Scala ~ 基本編 / 正規表現 ~
https://dk521123.hatenablog.com/entry/2023/03/18/034704
Scala ~ 基本編 / ジェネリック
https://dk521123.hatenablog.com/entry/2023/03/21/003817
Scala ~ Enum ~
https://dk521123.hatenablog.com/entry/2023/01/05/000000
Scala ~ ファイル名・パスの扱い ~
https://dk521123.hatenablog.com/entry/2023/03/11/000000
Scala ~ ファイルハンドリング ~
https://dk521123.hatenablog.com/entry/2023/01/03/000000
Scala ~ YAML ~
https://dk521123.hatenablog.com/entry/2023/03/16/012034
Scala ~ JDBC / DB接続 ~
https://dk521123.hatenablog.com/entry/2023/03/26/000950
Scala ~ AWS SDK ~
https://dk521123.hatenablog.com/entry/2023/03/24/211033
SBT ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/22/000000
SBT ~ 基本編 / build.sbt ~
https://dk521123.hatenablog.com/entry/2023/01/27/000000
SBT ~ 基本編 / sbtコマンド ~
https://dk521123.hatenablog.com/entry/2023/01/26/000000
SBT ~ sbtプラグイン ~
https://dk521123.hatenablog.com/entry/2023/01/25/000000
JavaでEmail ~ JavaMail / Text ~
https://dk521123.hatenablog.com/entry/2016/07/16/222422
JavaでEmail ~ JavaMail / 添付ファイル ~
https://dk521123.hatenablog.com/entry/2016/07/17/023459
JavaでEmail ~ SMTP認証 ~
https://dk521123.hatenablog.com/entry/2016/11/07/215251
JavaでEmail ~ SMTP認証 / DIGEST-MD5 ~
https://dk521123.hatenablog.com/entry/2016/12/07/222229
JavaでEmail ~ JavaMail / TLS ~
https://dk521123.hatenablog.com/entry/2017/05/03/163219
JavaでEmail ~ JavaMail / Return-Path・Errors-To ~
https://dk521123.hatenablog.com/entry/2017/05/07/000344
Amazon SES ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2017/04/28/234103
Amazon S3 ~ Boto3編 ~
https://dk521123.hatenablog.com/entry/2019/10/21/230004
Amazon S3 ~ Boto3でファイル存在チェック ~
https://dk521123.hatenablog.com/entry/2022/02/26/182526
AWS Glue ~ Scalaでの実装 ~
https://dk521123.hatenablog.com/entry/2023/03/17/000000
AWS Glue ~ ローカル環境を作成する / Glue v3.0版 ~
https://dk521123.hatenablog.com/entry/2022/01/31/165650
LocalStack ~ ローカルで疑似AWSを作成する ~
https://dk521123.hatenablog.com/entry/2019/12/14/010524
LocalStack ~ ローカルで疑似Lambda/S3/DynamoDBを作成する ~
https://dk521123.hatenablog.com/entry/2019/12/16/231149
SparkからSnowflakeへの接続について考える
https://dk521123.hatenablog.com/entry/2023/03/19/013833
Mockito ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2014/07/18/233904
Mockito ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2014/07/19/121409