ginkgo v2調査
DeferCleanup
を使うことで後処理をBeforeEachの中で書ける
用語
Spec Subjects = It
Contaniner Nodes = Describe/Context/When(この3つは機能的には等価)
Describe("Something expensive", Serial, func() {})
と書くことで並列実行時でも他プロセスとは同時に実行しないようにできる。
実行タイミングは並列実行できるスイートがすべて完了した後になる。
BeforeEach
と AfterEach
はすべてのスペックの実行前と実行後に動作するが、Contaniner Nodes内のスペックに対して一度だけ実行したい処理がある場合にも毎回実行されてしまう。(たとえばDBの初期化処理)
これはissueにもなっていた。
Contaniner Nodesの中スペックに対して一度だけ実行したい処理の場合はBeforeAll
とAfterAll
が使える。
ただし、Ordered Containers
に対してのみ使える。
Orderd Container
内のスペックは実行の順番がランダム化さらず記述された順に実行される。
また、Ordered
をBeforeEach/JustBeforeEach/AfterEach/JustAfterEach につけることでOrdered Container内のスペックに対して全体で1回だけ実行するようにできる。
Describe("Something", Ordered, func() {
BeforeEach(OncePerOrdered, func() {})
Context("context", Ordered, func() {
BeforeAll(func() {})
It("spec1", func() {})
It("spec2", func() {})
})
Context("context", , func() {
It("spec3", func() {})
It("spec4", func() {})
})
})
この場合はBeforeEachはOrdered Contextのスペックを実行する前に1回、spec3とspec4を実行する前に1回づつ実行される。
When we introduced this example we recommended condensing the tests into a single It and using By to document the test.
と公式ドキュメントにあるので公式的にはItは1つにしてByで分割するのをおすすめしてる?
Describeテーブルがトップレベルとしてサポートされたのでよりテーブルテストぽく書きやすくなった。
フィルタリング
ペンディング(常に実行されない)
// With the Pending decorator:
Describe("these specs aren't ready for primetime", Pending, func() { ... })
It("needs work", Pending, func() { ... })
It("placeholder", Pending) //note: pending specs don't require a closure
DescribeTable("under development", Pending, func() { ... }, ...)
Entry("this one isn't working yet", Pending)
// By prepending `P` or `X`:
PDescribe("these specs aren't ready for primetime", func() { ... })
XDescribe("these specs aren't ready for primetime", func() { ... })
PIt("needs work", func() { ... })
XIt("placeholder")
PDescribeTable("under development", func() {...}, ...)
XEntry("this one isn't working yet")
スキップ(条件をつけて実行しない)
It("should do something, if it can", func() {
if !someCondition {
Skip("Special condition wasn't met.")
}
...
})
フォーカス : 指定したスペックだけを実行する
// With the Focus decorator:
Describe("just these specs please", Focus, func() { ... })
It("just me please", Focus, func() { ... })
DescribeTable("run this table", Focus, func() { ... }, ...)
Entry("run just this entry", Focus)
// By prepending `F`:
FDescribe("just these specs please", func() { ... })
FIt("just me please", func() { ... })
FDescribeTable("run this table", func() { ... }, ...)
FEntry("run just this entry", ...)
ginkgo --label-filter=QUERY
コマンドでラベルを指定して実行をフィルタできる
スペックラベル : Describe("Storing books", Label("integration", "storage"), func() {
It("can save entire shelves of books to the central library", Label("network", "slow", "library storage"), func() {
// has labels [integration, storage, network, slow, library storage]
})
It("cannot delete books from the central library", Label("network", "library storage"), func() {
// has labels [integration, storage, network, library storage]
})
It("can check if a book is stored in the central library", Label("network", "slow", "library query"), func() {
// has labels [integration, storage, network, slow, library query]
})
It("can save books locally", Label("local"), func() {
// has labels [integration, storage, local]
})
It("can delete books locally", Label("local"), func() {
// has labels [integration, storage, local]
})
})
ファイル名によるフィルタリング
ginkgo --focus-file
と ginkgo --skip-file
コマンドでファイル名を指定して実行する。
説明文ベースによるフィルタリング
ginkgo --focus=REGEXP
と ginkgo --skip=REGEXP
コマンドでノードの説明を指定してフィルタして実行する。