Specs2 のコンテキストでは、純粋主義者は、単体テストを行うために単体テスト スタイルを使用する必要があると主張する場合があります。Specs2 の受け入れテスト スタイルは、受け入れテストを行うためのものです。それはちょっと明白に聞こえます;-)
しかし、私は単体テストを書くという受け入れテストのスタイルを好みます (主に一貫性のため)。これを行うべきではない技術的な理由はありますか?
すべてのテストを同じスタイルで書くという一貫性が気に入っていますが、ユニット テスト スタイルは、プロジェクト オーナー (それほど技術的な人ではない) にとってナビゲートするのが少し難しくなります。受け入れテスト スタイルでは、不足している機能に遭遇したときに新しいテストを追加できます。たとえば、次のようになります。
"Cool new feature" ! todo ^
以下の例からわかるように ( Specs2 サイトから改作)、特に仕様が大きくなるにつれて、受け入れテストのスタイルはオタクではない人にとってはもう少し読みやすくなり、懸念事項をより適切に分離することができます。また、より構成的なスタイルやライティング テストにつながる可能性もあります。
単体テストのスタイル:
import org.specs2.mutable._
class HelloWorldSpec extends Specification {
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size(11)
}
"start with 'Hello'" in {
"Hello world" must startWith("Hello")
}
"end with 'world'" in {
"Hello world" must endWith("world")
}
}
}
受け入れテストのスタイル:
import org.specs2._
class HelloWorldSpec extends Specification { def is =
"This is a specification to check the 'Hello world' string" ^
p^
"The 'Hello world' string should" ^
"contain 11 characters" ! e1^
"start with 'Hello'" ! e2^
"end with 'world'" ! e3^
"do something cool" ! todo^
"do something cooler" ! todo^
end
def e1 = "Hello world" must have size(11)
def e2 = "Hello world" must startWith("Hello")
def e3 = "Hello world" must endWith("world")
}
いつの日か、文字列補間 (または何か) と追加の解析ルーチンを使用して、さらに読みやすい DSL を持つ複数のファイルで終了することさえあるかもしれません。
こんにちはTest.specs2
s"
This is a specification to check the 'Hello world' string
=========================================================
The 'Hello world' string should
-------------------------------
- $e1 contain 11 characters
- $e2 && $e3 start with 'Hello' and end with 'world'
- $todo do something cool
- $todo do something cooler
"
MyAppSpec2.scala
import org.specs2._
class HelloWorldSpec extends Specification { def is = HelloTest.specs2
def e1 = "Hello world" must have size(11)
def e2 = "Hello world" must startWith("Hello")
def e3 = "Hello world" must endWith("world")
}