【Scala】specs2 ~ 基本編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2024/06/08/122708

の続き。

Scala の specs2 について、少しづつ書き溜めていく

目次

【1】単体テストスタイル
【2】受入テストスタイル
 1)ポイント
 2)isメソッド

【1】単体テストスタイル

* org.specs2.mutable.Specification を使う

import org.specs2.mutable.Specification

class HelloSpec extends Specification {
  "HelloWorld" should {

    "Say Hello!" in {
      Hello.sayHello("Mike") must_== "Hello, Mike!"
    }
  }
}

【2】受入テストスタイル

* org.specs2.Specification を使う

import org.specs2._
import specification._

class HelloSpec extends script.Specification with Grouped {
  // [1] isメソッドに仕様(スペック)を纏めて記述
  def is =
  // 「end」キーワードまで行末を「^」でつなげていく
    "This is a spec to check 'Hello World'" ^
      p^  // p はparagraph(パラグラフ・段落)の p
      "The 'Hello World' string should"       ^
      "contain 11 characters"               ! isLengthValid ^ // "description" ! body の形式で書く
      "start with 'Hello'"                  ! isStartValid ^
      "end with 'World'"                    ! isEndValid ^
      end

  // [2] 実際のテストは、仕様とは分けて記述
  val helloWorld = "Hello World"
  def isLengthValid = helloWorld must have size(11)
  def isStartValid =  helloWorld must startWith("Hello")
  def isEndValid = helloWorld must endWith("World")
}

出力結果

[info] HelloSpec
[info] This is a spec to check 'Hello World'                                                                                                        
[info] The 'Hello World' string should+ contain 11 characters+ start with 'Hello'+ end with 'World'                                                 
[info] Total for specification HelloSpec                                                                                                            
[info] Finished in 234 ms                                                                                                                           
[info] 3 examples, 0 failure, 0 error                                                                                                               
[info] Passed: Total 3, Failed 0, Errors 0, Passed 3
[success] Total time: 9 s, completed 2024/06/09 23:24:11

エラー時

[info] HelloSpec
[info] This is a spec to check 'Hello World'                                                                                                        
[error] x contain 11 characters                                                                                                                     
[error]  'Hello World' doesn't have size 13 but size 11 (HelloSpec.scala:26)                                                                        
[info] The 'Hello World' string should+ start with 'Hello'+ end with 'World'                                                                        
[info] Total for specification HelloSpec                                                                                                            
[info] Finished in 230 ms                                                                                                                           
[info] 3 examples, 1 failure, 0 error                                                                                                               
[error] Failed: Total 3, Failed 1, Errors 0, Passed 2
[error] Failed tests:                                                                                                                               
[error]         HelloSpec                                                                                                                           
[error] (Test / test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 8 s, completed 2024/06/09 23:25:33

1)ポイント

[1] isメソッドに仕様(スペック)を纏めて記述
[2] 実際のテストは、仕様とは分けて記述

2)isメソッド

* is メソッドは、 abstract になっており、これを実装する

def is = "title" ^ p ^ "describe1" ! example1 ^ "describe2" ! example2 ^ end

* fragmentは、「"title"」「p」「"describe1" ! example1」「"describe2" ! example2」「end」の5つ
*  「end」キーワードまで行末を「^」でつなげていく
* p は、paragraph(パラグラフ・段落)の p
* "description" ! body の形式で書く
Symbol Explanations
t タブ
bt バックタブ
br 空行
p br ^ bt
endbr end ^ br
endp end ^ p

参考文献

https://seratch.hatenablog.jp/entry/20120122/1327231056
https://eed3si9n.com/tetrix-in-scala/ja/specs2.html
https://qiita.com/erukiti/items/c7a3c0465ef771f21d1d
https://qiita.com/erukiti/items/abd4234b52d7b70e263f
https://techmedia-think.hatenablog.com/entry/20121206/1354789805
https://blog.magnolia.tech/entry/2017/12/06/001322
https://qiita.com/2KB/items/9d86fdfb8a268a05d114
https://tech-blog.tsukaby.com/archives/769/
https://tech-blog.tsukaby.com/archives/767/
https://seratch.hatenablog.jp/entry/20120122/1327231056

関連記事

Scala ~ テスティングフレームワーク
https://dk521123.hatenablog.com/entry/2024/06/07/183708
specs2 ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/06/08/122708
ScalaTest ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/27/001306
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
ScalaTest ~ with Coverage ~
https://dk521123.hatenablog.com/entry/2023/08/07/222945
Scala ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/03/10/193805
Scala ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/12/184331